如何修改下面的config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="OTMS.Properties.Settings.OTMSConnectionString" connectionString="Data Source=192.168.1.120;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
如何把connectionString="Data Source=192.168.1.120;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" 
改为 
connectionString="Data Source=192.168.1.121;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" 并且保存下来?
这个配置文件的名字叫App1.config,请高人指点

解决方案 »

  1.   

    修改程序本身的configxml.csusing System;
    using System.Configuration;
    using System.Reflection;
    using System.Web;
    using System.Xml;public enum ConfigFileType
    {
        WebConfig,
        AppConfig
    }namespace ShaIyaTool.App_Code
    {
        /// <summary>
        /// Summary description for ReadWriteConfig.
        /// </summary>
        public class ReadWriteConfig
        {
            public string docName = String.Empty;
            private XmlNode node = null;
            private int _configType;
            public string message;
            public int ConfigType
            {
                get { return _configType; }
                set { _configType = value; }
            }        #region GetValue
            public string GetValue(string key)
            {     //读取
                try
                {
                    XmlDocument cfgDoc = new XmlDocument();
                    loadConfigDoc(cfgDoc);
                    // retrieve the appSettings node  
                    node = cfgDoc.SelectSingleNode("//appSettings");
                    if (node == null)
                    {
                        throw new InvalidOperationException("appSettings section not found");
                    }
                    // XPath select setting "add" element that contains this key to remove      
                    XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
                    if (addElem == null)
                    {
                        message = "此key不存在!";
                        return message;
                    }
                    message = System.Configuration.ConfigurationSettings.AppSettings[key];
                    return message;
                }
                catch
                {
                    message = "操作异常,获取value值失败!";
                    return message;
                }
            }
            #endregion        #region SetValue
            public string SetValue(string key, string value)
            {    //增加
                XmlDocument cfgDoc = new XmlDocument();
                loadConfigDoc(cfgDoc);
                // retrieve the appSettings node   
                node = cfgDoc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new InvalidOperationException("appSettings section not found");
                }
                try
                {
                    // XPath select setting "add" element that contains this key       
                    XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
                    if (addElem != null)
                    {
                        message = "此key已经存在!";
                        return message;
                    }
                    // not found, so we need to add the element, key and value   
                    else
                    {
                        XmlElement entry = cfgDoc.CreateElement("add");
                        entry.SetAttribute("key", key);
                        entry.SetAttribute("value", value);
                        node.AppendChild(entry);
                    }
                    //save it   
                    saveConfigDoc(cfgDoc, docName);
                    message = "添加成功!";
                    return message;
                }
                catch
                {
                    message = "操作异常,添加失败!";
                    return message;
                }
            }        #endregion        #region saveConfigDoc
            private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
            {
                try
                {
                    XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
                    writer.Formatting = Formatting.Indented;
                    cfgDoc.WriteTo(writer);
                    writer.Flush();
                    writer.Close();
                    return;
                }
                catch
                {
                    throw;
                }
            }        #endregion        #region removeElement
            public string removeElement(string elementKey)
            { // 删除
                try
                {
                    XmlDocument cfgDoc = new XmlDocument();
                    loadConfigDoc(cfgDoc);
                    // retrieve the appSettings node  
                    node = cfgDoc.SelectSingleNode("//appSettings");
                    if (node == null)
                    {                    throw new InvalidOperationException("appSettings section not found");
                    }                XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']");
                    if (addElem == null)
                    {
                        message = "此key不存在!";
                        return message;
                    }
                    // XPath select setting "add" element that contains this key to remove      
                    node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
                    saveConfigDoc(cfgDoc, docName);
                    message = "删除成功!";
                    return message;
                }
                catch
                {
                    message = "操作异常,删除失败!";
                    return message;
                }
            }
            #endregion        #region modifyElement
            public string modifyElement(string elementKey, string elementValue)
            { //修改
                try
                {
                    XmlDocument cfgDoc = new XmlDocument();
                    loadConfigDoc(cfgDoc);
                    // retrieve the appSettings node  
                    node = cfgDoc.SelectSingleNode("//appSettings");
                    if (node == null)
                    {
                        throw new InvalidOperationException("appSettings section not found");
                    }
                    // XPath select setting "add" element that contains this key to remove      
                    XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']");
                    if (addElem == null)
                    {
                        message = "此key不存在!";
                        return message;
                    }                addElem.SetAttribute("value", elementValue);
                    //save it   
                    saveConfigDoc(cfgDoc, docName);
                    message = "修改成功!";
                    return message;
                }
                catch
                {
                    message = "操作异常,修改失败!";
                    return message;
                }
            }
            #endregion        #region loadConfigDoc
            private XmlDocument loadConfigDoc(XmlDocument cfgDoc)
            {
                // load the config file   
                if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
                {
                    docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
                    docName += ".exe.config";
                }
                else
                {
                    //docName=HttpContext.Current.Server.MapPath("web.config");
                }
                cfgDoc.Load(docName);
                return cfgDoc;
            }
            #endregion
        }
    }调用
    //读
    using ShaIyaTool.App_Code;
     ShaIyaTool.App_Code.ReadWriteConfig xml = new ShaIyaTool.App_Code.ReadWriteConfig();
                xml.ConfigType = (int)ConfigFileType.AppConfig;
                textBox1.Text = xml.GetValue("connectionString").ToString();//写 ReadWriteConfig config = new ReadWriteConfig();
                    config.ConfigType = (int)ConfigFileType.AppConfig;
                    config.modifyElement("connectionString", textBox1.Text);
      

  2.   

    错了,把 connectionString改成OTMS.Properties.Settings.OTMSConnectionString
      

  3.   

    高人在哪里?7楼的代码运行后的结果是:操作异常,获取value值失败!
      

  4.   

    那个类是给你用来修改你程序本身的config的
    比如你的应用程序名为text.exe,那你的config应该是text.exe.config
      

  5.   


    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
      <connectionStrings> 
        <add name="OTMS.Properties.Settings.OTMSConnectionString" connectionString="Data Source=192.168.1.120;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/> 
      </connectionStrings> 
    </configuration> 
    <!--如何把connectionString="Data Source=192.168.1.120;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" 
    改为 
    connectionString="Data Source=192.168.1.121;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123"-->
    <!--修改-->
    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
      <connectionStrings> 
        <add name="OTMS.Properties.Settings.OTMSConnectionString" connectionString="Data Source=192.168.1.121;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/> 
      </connectionStrings> 
    </configuration> 
    <!--然后保存就好了XML文件是可以直接修改的在打包的时候不需要打包进去-->
      

  6.   

    ....
    LZ好歹叁三角,难道还不知道直接修改...这种需求很常见啊,有些配置信息写在config里,需要程序可以去配置,比如LZ的数据库连接信息
    其实LZ可以把连接字符串分开,比如
    add key="server" value="127.0.0.1"
    add key="account" value="sa"
    add key="pwd" value="111111"
    ...LZ报的那个错就是因为你没这样做,我本以为你会变通考虑直接象我这样写的.
    然后在程序里根据这些信息再组成连接字符串,而同时又可以用自己的程序修改这些配置信息,就用我的那个类就可以
    我就是这样做的
      

  7.   


    //添加引用public static void SetConfig(string key,string val){
          Configuration config=ConfigurationManager.OpenExeConfiguration("");
          config.ConnectionStrings.ConnectionStrings[key].Value=val;
          config.Save();
    }string name="OTMS.Properties.Settings.OTMSConnectionString"
    string val="Data Source=192.168.1.121;Initial Catalog=OTMS;Persist Security Info=True;User ID=sa;Password=123"
    //调用
    SetConfig(name,val);
    //注意每次设置完必须重新启动程序配置文件才生效//程序没编译 有小问题见谅
      

  8.   

    建议直接更改   应用程序text.exe.config 下的数据, 否则每次更改完都必须重新启动程序才能生效。