C# WinForm修改配置文件


AppConfigPath 配置文件路径 ,注意 是exe运行的相对路径
 private static string AppConfigPath = "WinListen.exe.config";

        public static void AppSetValue(string AppKey, string AppValue)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(AppConfigPath);
            XmlNode xmlNode = xmlDocument.SelectSingleNode("//appSettings");
            XmlElement xmlElement = (XmlElement)xmlNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xmlElement != null)
            {
                xmlElement.SetAttribute("value", AppValue);
            }
            else
            {
                XmlElement xmlElement2 = xmlDocument.CreateElement("add");
                xmlElement2.SetAttribute("key", AppKey);
                xmlElement2.SetAttribute("value", AppValue);
                xmlNode.AppendChild(xmlElement2);
            }
            xmlDocument.Save(AppConfigPath);
        }

 


-------2013年11月5日  by LM

  

posted @ 2013-11-05 17:53  兴想事成  阅读(369)  评论(0编辑  收藏  举报