C # Read Modify compounding file (Framework2.0)

Source: Internet
Author: User
Setting and reading the configuration file in VS 2005 has become simple and strongly typed, and the read value can be assigned directly to the corresponding variable without casting.
For example: DateTime userDateTime1 = Properties.Settings.Default.userDateTime1; It's a lot of convenience. But have you found that using Properties.Settings.Default.Save () to save the settings, the application range of settings Why did not save the success of the user scope of the settings change does not reflect the app.config File?

1. Application Settings in VS 2005
Open Project Properties» settings, as shown below:

Enter a name, select the type and range, and enter the value save to complete the setting.
Types: Int,string,datetime and other types of data;
Scope: The application range setting is valid for all users;
User-scoped settings are valid for the current user (the user currently logged on by Windows), and each user can have a different value for the same setting, with no impact.

2. Read the configuration file (read application settings)
Whether it's a application range setting or a user-scoped setting, the Read method is the same.
Read settings
This.appSetting1TextBox.Text = Properties.Settings.Default.appSetting1;
This.userSetting1TextBox.Text = Properties.Settings.Default.userSetting1;
3. Save user-scoped profiles (save user-scoped application settings)//Save user-scoped settings
Properties.Settings.Default.userSetting1 = This.userSetting1TextBox.Text;
Properties.Settings.Default.Save (); The user scope profile is not saved under the application folder, but is saved here: X:\Documents and settings\windows Log on to the user \local Settings\Application Data.

4. Save Application scope configuration file (Save application scope application settings)
Saving the application scope configuration file is not as simple as saving the User scope profile, and direct Properties.Settings.Default.Save () is not possible. Because the settings for the application range are "read-only" at run time.     The method used here is to use XmlDocument to save the config file directly and then set it in Reload. Save Settings for Applicationi range
String configfilename = Application.executablepath + ". config";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();
Doc. Load (Configfilename);
string configstring = @ "configuration/applicationsettings/setconfig.properties.settings/setting[@name = ' AppSetting1 ']/value ';
System.Xml.XmlNode Confignode = doc. selectSingleNode (configstring);
if (Confignode!= null)
{
Confignode.innertext = This.appSetting1TextBox.Text;
Doc. Save (Configfilename);
Refreshes the application settings so that the latest values are read at the next read time.
Properties.Settings.Default.Reload ();
}


By the way: Use Properties.Settings.Default.Reset () to restore the default value for user-wide settings (Restore from app.config).

Source code Download:setconfig.rar

Turn from:http://www.cnblogs.com/anjou/archive/2007/03/27/690465.html

The following two methods are similar, excerpt down to do a backup study:

private void Saveconfig (string connenctionstring)
{
XmlDocument doc=new XmlDocument ();
Get the full path to the configuration file
String strfilename=appdomain.currentdomain.basedirectory.tostring () + ". exe.config";
Doc. Load (strFileName);
Find all elements with the name "add"
XmlNodeList Nodes=doc. getElementsByTagName ("add");
for (int i=0;i<nodes. count;i++)
{
Gets the key property of the current element
XmlAttribute Att=nodes[i]. attributes["Key"];
Determines whether the current element is a target element based on the first property of the element
if (Att. value== "ConnectionString")
{
Assign a value to the second property in the target element
Att=nodes[i]. attributes["value"];
Att. value=connenctionstring;
Break
}
}
Save the changes above
Doc. Save (strFileName);
}
==============================================================================
public static void SetValue (String appkey,string appvalue)
{
XmlDocument xdoc = new XmlDocument ();
Xdoc.load (System.Windows.Forms.Application.ExecutablePath + ". config");

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 (System.Windows.Forms.Application.ExecutablePath + ". config");
}


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.