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 ());
}
}