Fun App. Config

Source: Internet
Author: User

During Winform development, some configuration information is inevitably written to the APP. in the CONFIG file, after the program is generated, the APP. CONFIG is changed to A file with the program name + CONFIG. For example, if the program name is A, the generated APP. the CONFIG file will become. EXE. CONFIG file!

Directly add the code without explanation:


/// <Summary>
/// Configuration type
/// </Summary>
Public enum configType {etettings, connectionStrings}
 
Public static string GetConfig (configType cType, string keyOrName)
{
String result = string. Empty;
If (cType = configType. appSettings) result = System. Configuration. ConfigurationManager. etettings [keyOrName];
Else if (System. Configuration. ConfigurationManager. ConnectionStrings [keyOrName]! = Null)
Result = System. Configuration. ConfigurationManager. ConnectionStrings [keyOrName]. ConnectionString;
 
Return result = null? String. Empty: result;
}
/// <Summary>
/// Read the application configuration value. If no corresponding item exists, String. Empty is returned.
/// </Summary>
/// <Param name = "key"> Configure the ID code </param>
/// <Returns> </returns>
Public static string getdeleetask( string key)
{
Return GetConfig (configType. receivettings, key );
}
/// <Summary>
/// Modify or create a configuration item www.2cto.com
/// </Summary>
/// <Param name = "key"> Configure the ID code </param>
/// <Param name = "val"> configuration item value </param>
Public static void SetAppSettings (string key, string val)
{
SetConfig (configType. appSettings, key, val );
}
Public static void SetConfig (configType cType, string keyOrName, string val) // you can specify two parameters: the configuration file key name and the value of the key.
{
Try
{
XmlDocument xDoc = new XmlDocument (); // defines XmlDocument. This class is generally used to operate xml.
String path = biovision. ihosppath. his. dbCommon. db. configFilePath; // obtain the absolute path of the App. config file.
// String str = System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase; // This is the path for obtaining the configuration file
// Str = str. Substring (0, str. Length-10) + "App. config"; // This is the name of the configuration file.
 
 
XDoc. Load (path); // read xml
XmlNode xNode; // xml Node
XmlElement xElemCurrent; // xml Element
XmlElement xElemNew; // xml Element
If (cType = configType. connectionStrings)
{
XNode = xDoc. SelectSingleNode ("// connectionStrings ");
XElemCurrent = (XmlElement) xNode. SelectSingleNode ("// add [@ name = '" + keyOrName + "']");
If (xElemCurrent! = Null)
XElemCurrent. SetAttribute ("connectionString", val); // assign a value to this key if it exists.
Else
{// Add a key without this key and assign a value
XElemNew = xDoc. CreateElement ("add ");
XElemNew. SetAttribute ("name", keyOrName );
XElemNew. SetAttribute ("connectionString", val );
XElemNew. SetAttribute ("providerName", "System. Data. SqlClient ");
XNode. AppendChild (xElemNew );
}
}
Else
{
XNode = xDoc. SelectSingleNode ("// appSettings ");
XElemCurrent = (XmlElement) xNode. SelectSingleNode ("// add [@ key = '" + keyOrName + "']");
If (xElemCurrent! = Null)
XElemCurrent. SetAttribute ("value", val); // assign the value to AppValue if the key exists.
Else
{// Add a key without this key and assign a value
XElemNew = xDoc. CreateElement ("add ");
XElemNew. SetAttribute ("key", keyOrName );
XElemNew. SetAttribute ("value", val );
XNode. AppendChild (xElemNew );
}
}
XDoc. Save (path );
}
Catch (Exception e)
{
Log. LogException (e, "An error occurred while writing the config configuration file. ");
}
 
}
Later, I think about it. In fact, there is another way to implement the config read/write function (which is provided by MS and may be better)


Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
Config. etettings. Settings [key]. Value = value;
Config. Save (ConfigurationSaveMode. Modified );
ConfigurationManager. RefreshSection ("receivettings ");


This is an excerpt from the event where you want to complain about Dongfeng ~
 

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.