C # Read and modify the value of APP. config

Source: Internet
Author: User

In winform, you can add a configuration file (such as app. config) to set some parameters for the program, but to specify the location of the configuration file, you do not need to add vs, write two methods to read and modify the specified file.
Public class appsettings
{
Public static string appconfig ()
{
Return System. Io. Path. Combine (application. startuppath, "app. config"); // the configuration file is in the program directory.
}

Public static string getvalue (string appkey)
{
Xmldocument xdoc = new xmldocument ();
Try
{
Xdoc. Load (appsettings. appconfig ());
Xmlnode xnode;
Xmlelement xelem;
Xnode = xdoc. selectsinglenode ("// appsettings"); // Add it to your app. config file. <appsetting> </appsetting>
Xelem = (xmlelement) xnode. selectsinglenode ("// Add [@ key = '" + appkey + "']");
If (xelem! = NULL)
Return xelem. getattribute ("value ");
Else
Return "";
}
Catch (exception)
{
Return "";
}
}

Public static void setvalue (string appkey, string appvalue)
{
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (appsettings. appconfig ());
Xmlnode xnode;
Xmlelement xelem1;
Xmlelement xelem2;
Xnode = xdoc. selectsinglenode ("// appsettings ");
Xelem1 = (xmlelement) xnode. selectsinglenode ("// Add [@ key = '" + appkey + "']");
If (xelem1! = NULL)
{
Xelem1.setattribute ("value", appvalue );
}
Else
{
Xelem2 = xdoc. createelement ("add ");
Xelem2.setattribute ("key", appkey );
Xelem2.setattribute ("value", appvalue );
Xnode. appendchild (xelem2 );
}
Xdoc. Save (appsettings. appconfig ());
}

/// <Summary>
///
/// </Summary>
/// <Returns> </returns>
Public static xmlnode getaddressvalues ()
{
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (appconfig ());
Xmlnode xnode;
Xnode = xdoc. selectsinglenode ("// mainform-comboboxweb ");
Return xnode;
}

Public static void setaddressvalue (string appvalue)
{
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (appconfig ());
Xmlnode element;
Element = xdoc. selectsinglenode ("// mainform-comboboxweb ");
Xmlnode node = xdoc. createnode (xmlnodetype. element, "item ","");
Node. innertext = appvalue;
Element. appendchild (node );
Xdoc. Save (appconfig ());

}

}

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.