关于Configuration.ConfigurationManager

Configuration.ConfigurationManager是.Net2.0的新类。在winform必须添加对System.Configuration.dll的引用才可以调用。
在做winform配置文件的时候总是不能实时读取配置数据。因为web程序中的习惯,在web程序中配置文件更改后,应用程序会自动重启一次,于是配置会自动生效。但winform程序没有这个机制,于是Configuration.ConfigurationManager调用配置不会自动更新。

手工实现:抛弃Configuration.ConfigurationManager,直接读xml文档。
public string ReadAppSetting(string key)
        {
            
string xPath = "/configuration/appSettings//add[@key='"+key+"']";
            XmlDocument doc 
= new XmlDocument();
            
string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            doc.Load(exeFileName 
+ ".exe.config");
            XmlNode node 
= doc.SelectSingleNode(xPath);
            
return node.Attributes["value"].Value.ToString();
        }
posted @ 2008-06-22 15:19  小y  阅读(1358)  评论(0编辑  收藏  举报