在項目中我們會經常用到App.config文件,有的是自動生成的,比如引用webservice、wcf服務時生成;也有手動建立的配置文件直接默認名就為app.config。
這些配置有的保存當前程序集用到的一些可供外部改動的變量,比如:
- <configuration>
- <appSettings>
- <add key="keyName" value="value"/>
- </appSettings>
- </configuration>
這種的配置直接使用 ConfigurationManager.AppSettings["key名"]來讀取比較方便。例如:
- public class ReadConfig
- {
- public static string ConfigKeyValue
- {
- string config = ConfigurationManager.AppSettings["ConfigKeyValue"];
- config = string.IsNullOrEmpty(config) ? "空字符串" : config;
- return config;
- }
- }
有表示數據庫連接的比如ADO.NET Entity連接數據時會生成配置。
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
- </startup>
- <connectionStrings>
- <add name="OracleEntities" connectionString="" />
- </connectionStrings>
- </configuration>
有Microsoft.Practices.EnterpriseLibrary連接數據庫的配置 (http://blog.csdn.net/yysyangyangyangshan/article/details/8488791)。
再有
”混合模式程序集是針對“v2.0.50727”版的運行時生成的,在沒有配置其他信息的情況下,無法在 4.0 運行“錯誤時要用的,
- <?xml version="1.0"?>
- <configuration>
- <startup useLegacyV2RuntimeActivationPolicy="true">
- <supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
- <supportedRuntime version="v2.0.50727"/>
- </startup>
- </configuration>
等等,總之app.config作用很多。
但是在一個項目中很多程序集都要用到app.config該怎么辦呢?比如如下情況:

FouctionDll程序集中配置:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="key1" value="你好,世界!"/>
- </appSettings>
- </configuration>
主目錄TestAppConfig中的配置:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="key2" value="Hello,world!" />
- </appSettings>
- </configuration>
這樣的情況下,如果主程序集需要引用FouctionDll,配置被復制過來由於配置名重復,自然會被主程序的配置覆蓋。
還有就是如果FouctionDll中要引用遠程服務,會自動生成app.config,一旦主程序引用該配置依然無法使用。
針對這樣,應該做如下解決:
1、第一種情況,每一個程序集的配置是手動增加的話,將起名字改變。讀取方式不再使用
ConfigurationManager.AppSettings["key"]來讀取,可改為:
- public class ReadConfig
- {
- private static string currentConfig = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"ConfigName.config";
- /// <summary>
- /// 判斷是否有人操作的間隔時間
- /// </summary>
- public static string ConfigKeyValue
- {
- get
- {
- string time =GetAttributeValue(currentConfig,"ConfigKeyValue");
- if (string.IsNullOrEmpty(time))
- {
- return "180";
- }
- return time;
- }
- }
- /// <summary>
- /// 獲取配置文件的屬性
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- private static string GetAttributeValue(string file, string key)
- {
- string value = string.Empty;
- try
- {
- if (File.Exists(file))
- {
- XmlDocument xml = new XmlDocument();
- xml.Load(file);
- XmlNode xNode = xml.SelectSingleNode("//appSettings");
- XmlElement element = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
- value = element.GetAttribute("value").ToString();
- }
- }
- catch { }
- return value;
- }
- }
這種方式的好處是讓每個程序集相對獨立,缺點是如果是自動生成的app.config則還是會有上述問題。那么對於需要用到的名字必須是app.config的情況該如何呢?
可以使用超鏈接的方式,就是在項目中只有主程序使用app.config,其他程序集使用它的鏈接,這樣共同使用,如圖

讀取還是在當前目錄下使用ConfigurationManager.AppSettings["keyName"];
這種方式的好處是,可以解決了幾個程序集共用一個app.config的問題,缺點是程序集不獨立,因為引用了同一個文件,程序集在移動目錄是需要重新檢查再手動引用。
總之開發軟件第一目的是軟件功能正常,其次是我們開發時要盡可能的使自己方便,只有更方便,效率才能提高。
代碼下載:http://download.csdn.net/detail/yysyangyangyangshan/5004721
