110,561
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name ="lsh" connectionString ="000001"/>
</connectionStrings>
</configuration>
string path = System.Windows.Forms.Application.ExecutablePath + ".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
XmlNode xNode = xDoc.SelectSingleNode("//connectionStrings");
XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add[@name='" + "lsh" + "']");
if (xElem != null)
{
xElem.Attributes["connectionString"].Value = "0000002";
//xElem.SetAttribute("connectionString", "0000002");
}
xDoc.Save(path);
SetConfiguration#region SetConfiguration
/**//// <summary>
/// 设置appSetting的值
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="filePath">App.config文件路径</param>
public static void SetConfiguration(string key, string value,string filePath)
{
Configuration configuration = null; //Configuration对象
AppSettingsSection appSection = null; //AppSection对象
configuration = ConfigurationManager.OpenExeConfiguration(filePath);
//取得AppSetting节
appSection = configuration.AppSettings;
//赋值并保存
appSection.Settings[key].Value = value;
configuration.Save();
}
#endregion