因爲我發現通過使在System.Configuration的包裝類,如下
public class BaseConfiguration
{
protected static object GetAppSetting(Type expectedType, string key)
{
string value = ConfigurationManager.AppSettings.Get(key);
try
{
if (expectedType == typeof(int))
return int.Parse(value);
if (expectedType == typeof(string))
return value;
throw new Exception("Type not supported.");
}
catch (Exception ex)
{
throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.",
key, expectedType), ex);
}
}
}
我們訪問系統的方式應用程序設置變量,最好的辦法我們可以通過使用另一個類的硬編碼名稱訪問所需的設置變量,如下所示
public class ConfigurationSettings:BaseConfiguration
{
#region App setting
public static string ApplicationName
{
get { return (string)GetAppSetting(typeof(string), "ApplicationName"); }
}
public static string MailBccAddress
{
get { return (string)GetAppSetting(typeof(string), "MailBccAddress"); }
}
public static string DefaultConnection
{
get { return (string)GetAppSetting(typeof(string), "DefaultConnection"); }
}
#endregion App setting
#region global setting
#endregion global setting
}
就像我讀4倍MSDN的例子和文章。而在這裏降落了。只需添加一個參考文獻..他們爲什麼不能這麼說呢。好問題! +1 – ppumkin 2012-09-14 10:18:56
如果你想**寫回設置**,請看** [這裏](https://stackoverflow.com/a/11841175/1016343)**你如何做到這一點。 – Matt 2017-07-17 11:16:40
[AppSettings vs applicationSettings(.NET app.config/Web.config)]的優缺點](https://stackoverflow.com/questions/460935/pros-and-cons-of-appsettings-vs-applicationsettings配置網絡配置) – Matt 2017-07-17 11:17:36