Read the configuration file-AppConfig, configuration file-appconfig

Source: Internet
Author: User

Read the configuration file-AppConfig, configuration file-appconfig

Using System. xml; using System. IO; using System; namespace Framework. common {/// <summary> /// used to obtain or set the Web. config /*. exe. auxiliary class of node data in config /// </summary> public sealed class AppConfig {private string filePath; /// <summary> /// retrieve the Web from the current directory in sequence. config and *. app. config file. /// If one is found, use it as the configuration file; otherwise, an ArgumentNullException exception is thrown. /// </Summary> public AppConfig () {string webconfig = Path. combine (AppDomain. currentDomain. baseDirectory, "Web. config "); string appConfig = AppDomain. currentDomain. setupInformation. configurationFile. replace (". vshost "," "); if (File. exists (webconfig) {filePath = webconfig;} else if (File. exists (appConfig) {filePath = appConfig;} else {throw new ArgumentNullException ("no Web found. config file or application configuration Set file, please specify the configuration file ");}} /// <summary> /// specify the specific configuration file path /// </summary> /// <param name = "configFilePath"> Configuration File Path (absolute path)) public AppConfig (string configFilePath) {filePath = configFilePath ;} /// <summary> /// set the config file of the program /// </summary> /// <param name = "keyName"> key name /// <param name = "keyValue"> key value public void AppConfigSet (string keyName, string keyValue) {// because multiple Add key values exist, the operation to access the deleetting operation is unsuccessful. Therefore, comment the following statement and use the new method. /* String xpath = "// add [@ key = '" + keyName + "']"; XmlDocument document = new XmlDocument (); document. load (filePath); XmlNode node = document. selectSingleNode (xpath); node. attributes ["value"]. value = keyValue; document. save (filePath); */XmlDocument document = new XmlDocument (); document. load (filePath); XmlNodeList nodes = document. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// Obtain the key attribute XmlAttribute = nodes [I] of the current element. attributes ["key"]; // determines whether the current element is the target element if (attribute! = Null & (attribute. value = keyName) {attribute = nodes [I]. attributes ["value"]; // assign an if (attribute! = Null) {attribute. value = keyValue; break ;}} document. save (filePath) ;}//< summary> /// read the key value of the config file of the program. /// If the key name does not exist, return NULL // </summary> /// <param name = "keyName"> key name // <returns> </returns> public string AppConfigGet (string keyName) {string strReturn = string. empty; try {XmlDocument document = new XmlDocument (); document. load (filePath); XmlNodeList nodes = document. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute = nodes [I] of the current element. attributes ["key"]; // Determine whether the current element is the target element if (attribute! = Null & (attribute. Value = keyName) {attribute = nodes [I]. Attributes ["value"]; if (attribute! = Null) {strReturn = attribute. value; break ;}}} catch {;} return strReturn ;} /// <summary> /// obtain the value of the subitem in the specified key name /// </summary> /// <param name = "keyName"> key name /// <param name = "subKeyName"> use a semicolon (;) the subitem name that is the separator // The value of the <returns> corresponding subitem name (that is, the value after the = sign) </returns> public string GetSubValue (string keyName, string subKeyName) {string connectionString = AppConfigGet (keyName ). toLower (); string [] item = connectionStrin G. split (new char [] {';'}); for (int I = 0; I <item. length; I ++) {string itemValue = item [I]. toLower (); if (itemValue. indexOf (subKeyName. toLower ()> = 0) // if it contains the specified keyword {int startIndex = item [I]. indexOf ("="); // return item [I] at the beginning of the equal sign. substring (startIndex + 1); // obtain the Value after the equal sign as Value} return string. empty ;}# region common configuration item attributes /// <summary> /// obtain the permission system link from the configuration file (value of the configuration item HWSecurity) /// </summary> public st Ring HWSecurity {get {return AppConfigGet ("HWSecurity") ;}/// <summary> // System ID (value of System_ID) /// </summary> public string System_ID {get {return AppConfigGet ("System_ID ");}} /// <summary> /// Application name (value of the configuration item ApplicationName) /// </summary> public string AppName {get {return AppConfigGet ("ApplicationName ");}} /// <summary> /// software vendor name (value of configuration item Manufacturer) /// </summary> public string M Anufacturer {get {return AppConfigGet ("Manufacturer ");}} /// <summary> /// set the database link address of the Enterprise Library in the config file of the program. /// </summary> /// <param name = "keyName"> key name /// <param name = "keyValue"> key value public void SetConnectionString (string keyName, string keyValue) {XmlDocument document = new XmlDocument (); document. load (filePath); XmlNodeList nodes = document. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the name attribute XmlAttribute att = nodes [I] of the current element. attributes ["name"]; // determines whether the current element is the target element if (att! = Null & (att. Value = keyName) {att = nodes [I]. Attributes ["connectionString"]; if (att! = Null) {att. value = keyValue; break ;}} document. save (filePath );} /// <summary> /// read the database link address of the Enterprise Library in the config file of the program /// </summary> /// <param name = "keyName"> key name /// <returns> </returns> public string GetConnectionString (string keyName) {string strReturn = string. empty; try {XmlDocument document = new XmlDocument (); document. load (filePath); XmlNodeList nodes = document. getElementsByTag Name ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute att = nodes [I] of the current element. attributes ["name"]; // determines whether the current element is the target element if (att! = Null & (att. Value = keyName) {att = nodes [I]. Attributes ["connectionString"]; if (att! = Null) {strReturn = att. value; break ;}}} catch {;} return strReturn ;} /// <summary> /// obtain database configuration information /// </summary> /// <param name = "keyName"> node name /// <returns> </returns> public DatabaseInfo GetDatabaseInfo (string keyName) {string connectionString = GetConnectionString (keyName); return new DatabaseInfo (connectionString );} /// <summary> /// set database configuration information /// </summary> /// <param name = "keyName"> /// <param name = "databaseInfo "> public void SetDatabaseInfo (string keyName, databaseInfo databaseInfo) {SetConnectionString (keyName, databaseInfo. connectionString) ;}# endregion }}

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.