asp.net 读取并修改config文件实现代码(2)


///<summary>
///在*.exe.config文件中appSettings配置节增加一对键、值对
///</summary>
///<param></param>
///<param></param>
private static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false;
foreach (string key in ConfigurationManager.AppSettings)
{
if(key==newKey)
{
isModified = true;
}
}

// Open App.Config of executable
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// You need to remove the old settings object before you can replace it
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(newKey,newValue);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}



复制代码 代码如下:


/// <summary>
/// 写入Key,Value 到XML文件
/// </summary>
/// <param></param>
/// <param></param>
public static void SaveConfig(string Key,string Value)
{
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "App.config";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == Key)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];

att.Value = Value;
break;
}
}
//保存上面的修改
doc.Save(strFileName);

}

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjjpjp.html