我先对App.config中的value进行了修改并且成功了,然后在用ConfigurationManager.AppSettings.Get(key);方法来查结果得到的还是和开始没有修改一样,问一下这是怎么回事啊,我也用了 ConfigurationManager.RefreshSection("appSettings");也不行,这是怎么回事呢? 

解决方案 »

  1.   

    Config文件在正常情况下,运行时不可修改的 , 你所修改的是内存的值,而文件并没有改变
      

  2.   

    刷新之后再读取一遍
    ConfigurationManager.RefreshSection("appSettings")   
    sValue = ConfigurationManager.AppSettings(name) 
    http://stackoverflow.com/questions/590791/do-you-have-to-restart-a-windows-service-if-you-change-the-app-config/
      

  3.   

    请注意,vs编码环境里是不会保存修改的config文件中的值的,若你的程序生成可执行文件后(bin文件夹里),你再执行刚才生成的可执行程序,你查看config文件,要需改的值才会保存。
    所以不用担心你所说的。
      

  4.   


     /// <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;
                }
                try
                {
                    string XPath = "/configuration/appSettings/add[@key='?']";
                    XmlDocument domWebConfig = new XmlDocument();                domWebConfig.Load(filepath);
                    XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));                if (addKey == null)
                    {
                             MessageBox.Show("没有找到<add   key='" + key + "'>的配置节");
                        return;
                    }
                    addKey.Attributes["value"].InnerText = strValue;
                    domWebConfig.Save(filepath);
                    MessageBox.Show("数据库连接配置成功","信息提示");
                               }
                catch
                {
                           return;
                }        }
    类似这样的代码,
      

  5.   

    漏了路径变量string filepath=null;
     filepath =Application.StartupPath;
                filepath += "//App.config";