【Vegas改编】获取,更新web.config的值
//vegas add
public static void writeConfig(string item, string key, string value)
{
if (item == "")
{
item = "appSettings";
}
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
if (appSection.Settings[key] == null)
{
appSection.Settings.Add(key, value);
config.Save();
}
else
{
appSection.Settings.Remove(key);
appSection.Settings.Add(key, value);
config.Save();
}
}
public static void writeConfig(string item, string key, string value)
{
if (item == "")
{
item = "appSettings";
}
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
if (appSection.Settings[key] == null)
{
appSection.Settings.Add(key, value);
config.Save();
}
else
{
appSection.Settings.Remove(key);
appSection.Settings.Add(key, value);
config.Save();
}
}
使用web.config进行获取,更新,会丢掉session,而且更新后程序必须重新执行,所以,推荐以下方法:
public void SetXmlNodeValue(string key, string strValue)
{
/**************************************************************************\
* 设置AppConfig.xml中的键值,AppConfig.xml格式如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value=""/>
</appSettings>
</configuration>
* key:是key上中key值
* strValue:strValue是value的值
* 2006 09 14 Peter
\**************************************************************************/
string XPath = "/configuration/appSettings/add[@key='?']";
System.Xml.XmlDocument domWebConfig = new System.Xml.XmlDocument();
string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";
domWebConfig.Load(filepath);
System.Xml.XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
}
addKey.Attributes["value"].InnerText = strValue;
domWebConfig.Save(filepath);
}
public string GetXmlNodeValue(string key)
{
/**************************************************************************\
* 获取AppConfig.xml中的键值,AppConfig.xml格式如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value=""/>
</appSettings>
</configuration>
* key:是key上中key值
* 2006 09 14 Peter
\**************************************************************************/
string XPath = "/configuration/appSettings/add[@key='?']";
System.Xml.XmlDocument domWebConfig = new System.Xml.XmlDocument();
string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";
domWebConfig.Load(filepath);
System.Xml.XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
}
return addKey.Attributes["value"].InnerText.ToString();
}
{
/**************************************************************************\
* 设置AppConfig.xml中的键值,AppConfig.xml格式如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value=""/>
</appSettings>
</configuration>
* key:是key上中key值
* strValue:strValue是value的值
* 2006 09 14 Peter
\**************************************************************************/
string XPath = "/configuration/appSettings/add[@key='?']";
System.Xml.XmlDocument domWebConfig = new System.Xml.XmlDocument();
string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";
domWebConfig.Load(filepath);
System.Xml.XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
}
addKey.Attributes["value"].InnerText = strValue;
domWebConfig.Save(filepath);
}
public string GetXmlNodeValue(string key)
{
/**************************************************************************\
* 获取AppConfig.xml中的键值,AppConfig.xml格式如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value=""/>
</appSettings>
</configuration>
* key:是key上中key值
* 2006 09 14 Peter
\**************************************************************************/
string XPath = "/configuration/appSettings/add[@key='?']";
System.Xml.XmlDocument domWebConfig = new System.Xml.XmlDocument();
string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";
domWebConfig.Load(filepath);
System.Xml.XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
}
return addKey.Attributes["value"].InnerText.ToString();
}
喜欢请赞赏一下啦^_^