C # operation configuration file App. Config

Source: Internet
Author: User
Tags allkeys

C # operation configuration file app. config
Before the emergence of c #, applications usually use ini or xml as configuration files. However, after the emergence of c #, most programmers still choose to use a highly operable. config application configuration file. Therefore, to facilitate the operation, you usually need to write a general class. There are many such generic classes on the Internet, but I think it is very convenient for me to write this generic helper class.

Using system;
Using system. collections. generic;
Using system. text;
Using system. configuration;

Namespace schwann. commlibrary
{
Public class confighelper
{
/// <Summary>
/// Obtain the configuration file based on the key value
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Returns> </returns>
Public static string getconfig (string key)
{
String val = string. empty;
If (configurationmanager. apps tutorial ettings. allkeys. contains (key ))
Val = configurationmanager. receivetask[ key];
Return val;
}

/// <Summary>
/// Obtain all configuration files
/// </Summary>
/// <Returns> </returns>
Public static dictionary <string, string> getconfig ()
{
Dictionary <string, string> dict = new dictionary <string, string> ();
Foreach (string key in configurationmanager. receivettings. allkeys)
Dict. add (key, configurationmanager. deleetask[ key]);
Return dict;
}

/// <Summary>
/// Obtain the configuration file based on the key value
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Param name = "defaultvalue"> default value </param>
/// <Returns> </returns>
Public static string getconfig (string key, string defaultvalue)
{
String val = defaultvalue;
If (configurationmanager. etettings. allkeys. contains (key ))
Val = configurationmanager. receivetask[ key];
If (val = null)
Val = defaultvalue;
Return val;
}

/// <Summary>
/// Write the configuration file. If the node does not exist, it is automatically created.
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Param name = "value"> value </param>
/// <Returns> </returns>
Public static bool setconfig (string key, string value)
{
Try
{
Configuration conf = configurationmanager. openexeconfiguration (configurationuserlevel. none );
If (! Conf. etettings. settings. allkeys. contains (key ))
Conf. deleetask. settings. add (key, value );
Else
Conf. appsettings. settings [key]. value = value;
Conf. save ();
Return true;
}
Catch {return false ;}
}

/// <Summary>
/// Write the configuration file (created with a key value). It is automatically created if the node does not exist.
/// </Summary>
/// <Param name = "dict"> key value set </param>
/// <Returns> </returns>
Public static bool setconfig (dictionary <string, 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. etettings. settings. allkeys. contains (key ))
Conf. deleetask. settings. add (key, dict [key]);
Else
Conf. deleetask. settings [key]. value = dict [key];
}
Conf. save ();
Return true;
}
Catch {return false ;}
}
}
}

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.