在程序运行的时候来设置里面的值.写的详细者给分

解决方案 »

  1.   

    好象是ConfigurationSettings.AppSettings
    Add或Remove------------------------------------
    我的团队:www.51team.com欢迎访问,有志者共谋事,每天都有惊喜,SOHO
      

  2.   

    App.config内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="ServerAddress" value="http://localhost:90/DataService.asmx"></add>
      </appSettings>
    </configuration>现在要修改ServerAddress的值,如下写:
    System.Configuration.ConfigurationSettings.AppSettings.Set("ServerAddress","http://127.0.0.1:90/DataService.asmx");
      

  3.   

    加入一个新项:
    System.Configuration.ConfigurationSettings.AppSettings.Add("test1","test1")删除一个原有项:
    System.Configuration.ConfigurationSettings.AppSettings.Remove("ServerAddress")
      

  4.   

    程序运行时写的话通常用读写一般XML文档的方式去读写:System.Xml.XmlDocument类。
    不过要注意,System.Configuration.ConfigurationSettings是在第一次读取时将值载入的,而后缓存在程序中。于是你在程序运行时改变了config文件的值后,在程序没有重新运行的情况下再次通过System.Configuration.ConfigurationSettings读取其值的话还是第一次读取时所缓存的值。
    所以如果你在程序运行过程中改变了值,又希望读到最新的值的话似乎只有手工通过System.Xml.XmlDocument类来读,通过System.Configuration.ConfigurationSettings读到的只是缓存了的最初的值。
    当然,程序再次运行就会重新读了。
      

  5.   

    报个错"集合是只读的".我的app.config文件不是只读的啊.
      

  6.   

    hainang1234(鼠·神·泪)
    说得够清楚了
      

  7.   

    我碰到过这样的需求,自己用System.Xml中的类库写了读写程序,如果你的app.config要求比较复杂,.net自己提供了一个Enterprise Library包,里面有完整的configuration组件可以使用。
    还有,你提到的app.config应该不会被锁定的。
      

  8.   

    System.Configuration.ConfigurationSettings.AppSettings.Set("ServerAddress","http://127.0.0.1:90/DataService.asmx");我是说这句会报错
      

  9.   

    private void UpdateConfig(string Xvalue)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(Application.ExecutablePath+".config");
    XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
    XmlElement ele = (XmlElement)node;
    ele.SetAttribute("value",Xvalue);
    doc.Save(Application.ExecutablePath+".config"); 
    }
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="ServerName" value=""/>
    </appSettings>
    </configuration>