C # Operations profile in details of App. Config

Source: Internet
Author: User
Tags allkeys

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///&LT;/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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.