程序配置文件如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="DPCUI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="RemotingIp" connectionString="192.168.249.166" />
    <add name="RemotingPort" connectionString="6777" />    <add name="MQPath" connectionString="FormatName:DIRECT=TCP:192.168.253.63\private$\monitor" />
  </connectionStrings>
</configuration>读得时候可以这样
RemotingIP = ConfigurationManager.ConnectionStrings["RemotingIp"].ConnectionString; 
如果想改RemotingIp的值,应该怎么写?

解决方案 »

  1.   

    就是一个xml文件,用xmldocumnet操作
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/
      

  2.   

    这个函数主要使用xmldocument来解析web.config.并用selectsinglenode()方法来定位要修改的配置节。要注意的是最后程序要save(),所以,你的apsnet帐号必须对web.config拥有写权限. 
    /// <summary> 
    /// 修改web.config文件appsettings配置节中的add里的value属性 
    /// </summary> 
    /// <res> 
    /// 注意,调用该函数后,会使整个web application重启,导致当前所有的会话丢失 
    /// </res> 
    /// <param name="key">要修改的键key</param> 
    /// <param name="strvalue">修改后的value</param> 
    /// <exception cref="">找不到相关的键</exception> 
    /// <exception cref="">权限不够,无法保存到web.config文件中</exception> 
    public void Modify(string key, string strvalue)
    {
        string xpath = "/configuration/appSettings/add[@key=?]";
           XmlDocument domwebconfig = new XmlDocument();       domwebconfig.Load(HttpContext.Current.Server.MapPath("/web.config"));
           XmlNode addkey = domwebconfig.SelectSingleNode((xpath.Replace("?", key)));
        if (addkey == null)
           {
            throw new ArgumentException("没有找到<add key=" + key + " value=.../>的配置节");
           }
           addkey.Attributes["value"].InnerText = strvalue;
           domwebconfig.Save(HttpContext.Current.Server.MapPath("/web.config"));
    }