close
文章出處

首先假設你的應用程序配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="name" value="old"/>

  </appSettings>

</configuration>

 

Ok,那么如何在運行時去修改name的值呢??

有很多童鞋會說可以使用Xml讀取配置文件,然后xxx。。。。

當然這種方法肯定可以解決問題,有沒有其他方法呢??

在這里我要介紹一種比較簡單的方法,可能已經有人知道了,那就是使用ConfigurationManager

ConfigurationManager 存在System.Configuration.dll 中。

代碼如下:

public static void Main()
{
    Console.WriteLine(ConfigurationManager.AppSettings["name"]);
    ChangeConfiguration();
    Console.WriteLine(ConfigurationManager.AppSettings["name"]);
    Console.ReadLine();
}

private static void ChangeConfiguration()
{
    //讀取程序集的配置文件
    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;

    Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
    //獲取appSettings節點
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
    
    //刪除name,然后添加新值
    appSettings.Settings.Remove("name");
    appSettings.Settings.Add("name", "new");

    //保存配置文件
    config.Save();
}

代碼很簡單:首先讀取配置文件,接著獲取appSettings節點,然后修改,接著保存。

運行:結果如下:

clip_image002

可以看到輸出的值是兩個old.

為什么??

查找msdn文檔可以發現微軟出于性能考慮,對ConfigurationManager采用了緩存策略,所以如果要讀取新的值,應該使用ConfigurationManagerRefreshSection來進行刷新,

ConfigurationManager . RefreshSection:

刷新命名節,這樣在下次檢索它時將從磁盤重新讀取它。

于是將Main方法修改為:

Console.WriteLine(ConfigurationManager.AppSettings["name"]);

ChangeConfiguration();

ConfigurationManager.RefreshSection("appSettings");

Console.WriteLine(ConfigurationManager.AppSettings["name"]);

重新清理解決方案,重新運行:

clip_image004

可以看到,仍然是兩個old。。。

為什么??  

難道值沒有修改??,我們打開應用程序的配置文件,可以通過監視assemblyConfigFile獲得路徑

上面是xxx\bin\Debug\CAStudy.exe.,對應的配置文件就是CAStudy.exe.config

clip_image006

文件的內容如下:

clip_image008

 

可以發現value 值已經更改,那么為什么輸出還是old,old 呢??

為了驗證不是VS2010的問題。

首先手動將CAStudy.exe.config 文件中的value改為”old”,接著再次運行CAStudy.exe 結果如下:

clip_image010

 

可以看到輸出時old,和new。為什么會這樣???

難道調試時讀取的不是修改的配置文件,或者修改的配置文件并不是調試的應用程序讀取的文件??

assemblyConfigFile 中設置斷點,可以發現assemblyConfigFile 讀取的是CAStudy.exe.Config。但是vs調試的時候運行的是CAStudy.vshost.exe。也就是說我們使用ConfigurationManager.OpenExeConfiguration 打開的是CAStudy.exe.config文件,但是我們調試的應用程序CAStudy.vshost.exe使用的是CAStudy.vshost.exe.config文件。

那么還有其他的方式可以準確的獲取應用程序配置文件嗎??

有的,使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

ChangeConfiguration()方法修改如下:

private static void ChangeConfiguration()
{

    //讀取程序集的配置文件
    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
    string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
     //獲取appSettings節點
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
    
    //刪除name,然后添加新值
    appSettings.Settings.Remove("name");
    appSettings.Settings.Add("name", "new");

    //保存配置文件
    config.Save();
}

清理,重新運行:

使用默認的不傳遞字符串的版本就可以打開當前配置文件了。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

如果要查看當前配置文件的完整路徑可以使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

 

重新運行,結果如下:

clip_image012

 

另外值得一提的是:ConfigurationManager.RefreshSection 不支持section Group.所以對于WCF的服務,你必須一個一個的RefreshSection:

 

ConfigurationManager.RefreshSection("system.serviceModel/behaviors");

ConfigurationManager.RefreshSection("system.serviceModel/bindings");

ConfigurationManager.RefreshSection("system.serviceModel/client");

ConfigurationManager.RefreshSection("system.serviceModel/services");

參考文章:How to Change .NET Configuration Files at Runtime (including for WCF)


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()