APP.COFIG的值如下<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyGMTool.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <MyGMTool.Properties.Settings>
            <setting name="mysqlName" serializeAs="String">
                <value>root</value>
            </setting>
            <setting name="mysqlPassword" serializeAs="String">
                <value />
            </setting>
            <setting name="mysqlAccount" serializeAs="String">
                <value>account</value>
            </setting>
            <setting name="mysqlAddress" serializeAs="String">
                <value>localhost</value>
            </setting>
            <setting name="mysqlMy" serializeAs="String">
                <value>my</value>
            </setting>
            <setting name="Conn2" serializeAs="String">
                <value>True</value>
            </setting>
        </MyGMTool.Properties.Settings>
    </userSettings>
</configuration>
我想在一个按妞时间里面修改这些值..                <value>True</value>里面的东西.`.

解决方案 »

  1.   

    直接修改Settings里的值不好吗?
      

  2.   

    使用System.Configuration可以读Setting但是不能写。我的办法是自己解析这个XML文件,然后通过代码修改。记住修改完毕后要调用ConfigurationManager.RefreshSection来更新相关的Section应用你的修改。
      

  3.   

    当作普通的xml文件来做修改吧.已知有一个XML文件(bookstore.xml)如下:
    <?xml version="1.0" encoding="gb2312"?>
    <bookstore>
      <book genre="fantasy" ISBN="2-3631-4">
        <title>Oberon's Legacy</title>
        <author>Corets, Eva</author>
        <price>5.95</price>
      </book>
    </bookstore>将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
    XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
       foreach(XmlNode xn in nodeList)//遍历所有子节点
       {
        XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
        if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
        {
         xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
     
         XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
         foreach(XmlNode xn1 in nls)//遍历
         {
          XmlElement xe2=(XmlElement)xn1;//转换类型
          if(xe2.Name=="author")//如果找到
          {
           xe2.InnerText="亚胜";//则修改
           break;//找到退出来就可以了
          }
         }
         break;
        }
       }
     
       xmlDoc.Save("bookstore.xml");//保存。最后结果为:
    <?xml version="1.0" encoding="gb2312"?>
    <bookstore>
      <book genre="fantasy" ISBN="2-3631-4">
        <title>Oberon's Legacy</title>
        <author>Corets, Eva</author>
        <price>5.95</price>
      </book>
      <book genre="update李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>亚胜</author>
        <price>58.3</price>
      </book>
    </bookstore>
      

  4.   


      ///   <summary>   
      ///向.config文件的appKey结写入信息AppValue   保存设置   
      ///   </summary>   
      ///   <param   name="AppKey">节点名</param>   
      ///   <param   name="AppValue">值</param>   
      private   void   SetValue(string   AppKey,string   AppValue)   
      {   
        //System.Configuration.ConfigurationSettings.AppSettings.Set(AppKey,AppValue);这个没用   
      XmlDocument   xDoc   =   new   XmlDocument();   
      xDoc.Load(System.Windows.Forms.Application.ExecutablePath   +   ".config");   
      XmlNode   xNode;   
      XmlElement   xElem1;   
      XmlElement   xElem2;   
      xNode   =     xDoc.SelectSingleNode("//appSettings");   
      xElem1   =   (XmlElement)xNode.SelectSingleNode("//add[@key='"   +   AppKey   +   "']");   
      if   (   xElem1   !=   null   )   xElem1.SetAttribute("value",AppValue);   
      else   
      {   
      xElem2   =   xDoc.CreateElement("add");   
      xElem2.SetAttribute("key",AppKey);   
      xElem2.SetAttribute("value",AppValue);   
      xNode.AppendChild(xElem2);   
      }   
      xDoc.Save(System.Windows.Forms.Application.ExecutablePath   +   ".config");   
      }