C # manually read and write the app config source code,

Source: Internet
Author: User

C # manually read and write the app config source code,

Public class ConfigOperator {public string strFileName; public string configName; public string configValue; public ConfigOperator () {// TODO: add the constructor logic //} public string ReadConfig1 (string configKey) {configValue = ""; configValue = ConfigurationSettings. appSettings ["+ configKey +" "]; return configValue;} // obtain the config file name of the program and its full path public void SetConfigName (string strConfigName) {configName = strConfigName; // obtain the full path of the configuration file GetFullPath ();} public void GetFullPath () {// obtain the full path of the configuration file strFileName = AppDomain. currentDomain. baseDirectory. toString () + configName;} public void SaveConfig (string configKey, string configValue) {XmlDocument doc = new XmlDocument (); doc. load (strFileName); // find all elements named "add" XmlNodeList nodes = doc. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute att = nodes [I] of the current element. attributes ["key"]; // determines whether the current element is the target element if (att. value = "" + configKey + "") {// assign att = nodes [I] to the second attribute in the target element. attributes ["value"]; att. value = configValue; break ;}// Save the modified doc above. save (strFileName);} public string ReadConfig (string configKey) {string tempStr = ""; XmlDocument doc = new XmlDocument (); doc. load (strFileName); // find all elements named "add" XmlNodeList nodes = doc. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute att = nodes [I] of the current element. attributes ["key"]; // determines whether the current element is the target element if (att. value = "" + configKey + "") {// assign att = nodes [I] to the second attribute in the target element. attributes ["value"]; tempStr = att. value ;}}// Save the above modification return tempStr ;}}

  

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.