app.config的读写、更新问题 两篇链接

有关App.config的基础讲解:读取并修改App.config文件

XML文件更新appsettings方法:修改App.config配置文件(项目总结)

整理后的更新方法:

        public static void SetValueByKey(string newKey, string newValue)
        {
            
try
            {
                
string filePath = new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName + "\\App.Config";
                
//建立一个XmlDocument对象
                XmlDocument webconfigDoc = new XmlDocument();
                
//找到节点的xml路径 
                string xPath = "/configuration/appSettings/add[@key='" + newKey + "']";
                
//加载web.config文件 
                webconfigDoc.Load(filePath); //读出路径放到txtfilepath文本框中
                XmlNode passkey = webconfigDoc.SelectSingleNode(xPath);
                passkey.Attributes[
"value"].InnerText = newValue;

                
//保存设置
                webconfigDoc.Save(filePath);
            }
            
catch (Exception ex)
            {
                
throw ex;
            }
        }

 

posted on 2009-12-28 11:52  onlyfew  阅读(375)  评论(0编辑  收藏  举报

导航