c# Operation Profile App. Config" detailed
Using system;using system.collections.generic;using system.text;using system.configuration;namespace schwann.commlibrary{public class ConfigHelper {//<summary>///For Profile///</SU mmary>//<param name= "key" > Key value </param>//<returns></returns> public STA Tic string GetConfig (String key) {string val = string. Empty; if (ConfigurationManager.AppSettings.AllKeys.Contains (key)) val = Configurationmanager.appsettings[key]; return Val; }///<summary>//Get all profiles///</summary>//<returns></returns> public static dictionary<string, string> GetConfig () {dictionary<string, string> dict = new dictionary<string, string> (); foreach (string key in ConfigurationManager.AppSettings.AllKeys) Dict. ADD (Key, Configurationmanager.apPsettings[key]); return dict; }///<summary>//////</summary>//<param name= "key" > Key value </p aram>//<param name= "defaultvalue" > Default value </param>//<returns></returns> P Ublic static string GetConfig (string key, String defaultvalue) {string val = defaultvalue; if (ConfigurationManager.AppSettings.AllKeys.Contains (key)) val = Configurationmanager.appsettings[key]; if (val = = null) val = defaultvalue; return Val; }///<summary>///write config file, if node does not exist automatically create///</summary>//<param name= "key" > Key value </param>//<param name= "value" > Value </param>//<returns></returns> PU Blic static bool Setconfig (string key, String value) {try {Configuration con f = ConfIgurationmanager.openexeconfiguration (Configurationuserlevel.none); if (!conf. AppSettings.Settings.AllKeys.Contains (Key)) Conf. APPSETTINGS.SETTINGS.ADD (key, value); Else Conf. Appsettings.settings[key]. Value = value; Conf. Save (); return true; } catch {return false;} }///<summary>///write config file (created with key value) and automatically create if node does not exist///</summary>//<param name= "D ICT "> Key values Collection </param>//<returns></returns> public static bool Setconfig (dictionary<st Ring, String> dict) {try {if (dict = = NULL | | dict. Count = = 0) return false; Configuration conf = configurationmanager.openexeconfiguration (Configurationuserlevel.none); foreach (String key in Dict. Keys) {if (!conf. Appsettings.sEttings. Allkeys.contains (Key)) Conf. APPSETTINGS.SETTINGS.ADD (Key, Dict[key]); Else Conf. Appsettings.settings[key]. Value = Dict[key]; } conf. Save (); return true; } catch {return false;} } }}
The previous article learned how to handle the configuration file read, but did not learn about the <appSettings> that is often used, but the reading of these parameters is much simpler:
Suppose you have the following configuration parameters
<appSettings> <add key= "Address" value= "China"/> </appSettings>
Read:
String address=system.configuration.configurationmanager.appsettings["Address". ToString ();
The truth is, it's so simple
The above is the C # operation configuration file in the detailed content of App. Config, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!