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 ;}
}
}
}