我的代码如下:
//修改App.Config中的数据库连接字符串
        public static void EditConfig(string key, string keyValue)//编辑参数
        {
            XmlDocument xmlDoc = new XmlDocument();
            string configPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("bin")) + "App.config";
            //string configPath = Application.ExecutablePath + ".config";            xmlDoc.Load(configPath);
            //这里以ConnectionString为例
            XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/connectionStrings/add[@name='" + key + "']");
            if (xmlNode.Attributes["connectionString"].Value == string.Empty)
            {
                xmlNode.Attributes["connectionString"].Value = keyValue;
                xmlDoc.Save(configPath);
            }
        }
这个方法是直接修改App.config文件,会跳出文件保存的提示。
有没有什么方法可以避免保存提示或者怎么能直接选择“是”然后程序继续。

解决方案 »

  1.   

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      config.AppSettings.Settings.Remove(key);
      config.AppSettings.Settings.Add(key, value);
      config.Save(ConfigurationSaveMode.Modified);
      ConfigurationManager.RefreshSection("appSettings");  
     
      

  2.   

    为什么添加了System.Configuration.dll,加入了using System.Configuration还是用不了Configuration和ConfigurationManager