public class DoConfig
    {
        public static void Editconfig(string Key,string Value)                      
        {
            XmlDocument xmldoc = new XmlDocument();
            string filePath = Application.ExecutablePath + ".config";
            xmldoc.Load(filePath);

            XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "appsettings")     //找到要改写的字段名 
                {
                    XmlNodeList _node = element.ChildNodes;
                    if (_node.Count > 0)
                    {
                        foreach (XmlElement el in _node)
                        {
                            //el.Attributes[Key].Value = Value;                                                                 //查找并改写 
                            if (el.Attributes["key"].Value == Key)
                                el.Attributes["value"].Value = Value;
                        }
                    }
                }
            }
            xmldoc.Save(filePath);       //保存改写文件 
        }
    }
posted on 2007-12-17 15:39  老刘老矣  阅读(461)  评论(0编辑  收藏  举报