修改APP.CONFIG问题
自己用winform写了个程序,想修改另外不在同一磁盘目录下的一个应用程序myapp的配置文件,窗体中有一个文本框,一个选择按钮,一个修改按钮,选择按钮用以选择myapp的安装目录,修改按钮用以修改myapp.exe.config中的值,其中有appsetting节中,有这样的键值<add key="ClientCode" value="11000110000000" />,想用修改按钮实现修改其中的value值,不知道怎么实现,在网上找到不少,但都没看太懂,初学C#,真不知道怎么处理,请指点一下
[解决办法]
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="strValue"></param> public void Modify(string key, string strValue) //两个参数:要修改的键值 和 要修改的新值; { string flagstr = strValue; if (strValue == string.Empty) { MessageBox.Show("连接串不能为空!"); return; } //string XPath = "/configuration/userInfo/add[@key='?']"; try { string XPath = "/configuration/appSettings/add[@key='?']"; XmlDocument domWebConfig = new XmlDocument(); //domWebConfig.Load((HttpContext.Current.Server.MapPath("web.config"))); domWebConfig.Load(filepath); XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key))); if (addKey == null) { //Response.Write("<script>alert (\"没有找到<add key='" + key + "' value=.../>的配置节\")</script>"); MessageBox.Show("没有找到<add key='" + key + "'>的配置节"); return; } addKey.Attributes["value"].InnerText = strValue; domWebConfig.Save(filepath); MessageBox.Show("数据库连接配置成功","信息提示"); } catch { } }