C # source code for manually reading and writing app config

Source: Internet
Author: User

Public class configoperator
{
Public String strfilename;
Public String configname;
Public String configvalue;
Public configoperator ()
{
//
// Todo: add the constructor logic here
//
}

Public String readconfig1 (string configkey)
{
Configvalue = "";
Configvalue = configurationsettings. deleettings ["" + configkey + ""];
Return configvalue;
}
 
// GetProgramThe name of the config file 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 of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = "" + configkey + "")
{
// Assign values to the second attribute of the target Element
ATT = nodes [I]. attributes ["value"];
Att. value = configvalue;
Break;
}
}
// Save the modification above
Doc. 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 of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = "" + configkey + "")
{
// Assign values to the second attribute of the target Element
ATT = nodes [I]. attributes ["value"];
Tempstr = Att. value;
}
}
// Save the modification above
Return tempstr;
}

}

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.