我用VS2008做的是C#windows窗体的应用程序,想实现程序在生成可执行文件之后,有这么一个功能可以动态配置数据库连接的参数,程序在安装之后更灵活的配置数据库连接参数。是做个可以动态修改App.config文件的窗体吗,还有什么思路,有可以参考的代码吗?

解决方案 »

  1.   

    动态加载或者动态修改app.config都可以的。程序修改vapp.config文件的方法:
     public static void SetKeyValue(string AppKey, string AppValue)
            {
                XmlDocument xDoc = new XmlDocument();            xDoc.Load(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.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("name", AppKey);
                   xElem2.SetAttribute("connectionString", AppValue);
                    xNode.AppendChild(xElem2);
                }*/
                xDoc.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.config");
            }