Configuration file--app.config file read and modify

Source: Internet
Author: User

As a normal XML file read, you first need to know how to find the path to the file. We know that the general configuration file is in the same directory as the executable EXE file, and only after the name is added. config Therefore, it can be obtained by means of application.executeablepath+ ". Cofig". However, it is more recommended to use AppDomain.CurrentDomain.SetupInformation.ConfigurationFile to directly obtain the location of the current program's configuration file, for specific reasons, and then described.

Now go back or look at the function above, look at its last line, what is its function?

Finding the MSDN documentation reveals that Microsoft has adopted a caching policy for Profile App. Config for performance reasons, although the above function does modify the value of the node in the disk file, but when it reads with the previous function, it will still get the original value, as if it had not been modified! So, you have to use this sentence, to do a refresh, forcing the next time the program reads, read from the disk file!

OK, now use Visual Studio to write C # program Children's shoes should have encountered an egg pain problem, is in the debug, obviously in the program to modify the configuration file, but the next time you re-execute the program, found that the program has not changed, Open the config file corresponding to the EXE file view, found that the file does not change!!!! Clearly is as an XML file to operate, how can this?!

In fact, this involves the operating mechanism of VS, the careful children's shoes will be in the EXE file in the same directory, found that there is a corresponding vshost.exe, as well as the Vshost.exe.config file, when you open this config file here will find that The value of the XML file in this area has changed! On the drip ~vs, whether in debug or release, running program is this with Vshost program, modify the program is also the corresponding CONFIG. When the program has just started, but is read the original and EXE file corresponding config file, the config file content to replace the original and Vshost.exe corresponding config inside the content, which is why each time after the resumption of the program after the reason for the original.

Because the program runs in VS, the program is not the same as the program that runs directly to the Bin folder, so It is more recommended to use AppDomain.CurrentDomain.SetupInformation.ConfigurationFile to get the current running program's configuration file.

There are various ways to change the contents of the App. Config, although Vshost.exe.config does modify it. But the next time you start to read the original config, and also replace the modified Vshost.exe.config

Finally, the following options are adopted:

    Public classAppconfighelper { Public Static stringGetvaluebykey (stringkey) {Configurationmanager.refreshsection ("appSettings"); returnConfigurationmanager.appsettings[key]; }         Public Static voidModifyappsettings (stringStrkey,stringvalue) {            //get the full path of the configuration file            varAssemblyconfigfile =assembly.getentryassembly ().            Location; varAppdomainconfigfile =AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;            Changeconfiguration (strkey, value, assemblyconfigfile);        Modifyappconfig (strkey, value, appdomainconfigfile); }        Private Static voidModifyappconfig (stringStrkey,stringValuestringConfigpath) {            varDoc =NewXmlDocument (); Doc.            Load (Configpath); //find all elements with the name "add"            varnodes = Doc. getElementsByTagName ("Add");  for(inti =0; I < nodes. Count; i++)            {                //gets the key property of the current element                varXmlAttributeCollection =Nodes[i].                Attributes; if(XmlAttributeCollection! =NULL)                {                    varATT = xmlattributecollection["Key"]; if(Att = =NULL)Continue; //determines whether the current element is a target element based on the first attribute of the element                    if(Att. Value! = strkey)Continue; //assign a value to the second attribute in the target elementATT = xmlattributecollection["value"]; Att. Value=value; }                 Break; }            //Save the changes aboveDoc.            Save (Configpath); Configurationmanager.refreshsection ("appSettings"); }         Public Static voidChangeconfiguration (stringKeystringValuestringpath) {            varConfig =configurationmanager.openexeconfiguration (path); Config.            AppSettings.Settings.Remove (key); Config.            APPSETTINGS.SETTINGS.ADD (key, value); Config.            Save (configurationsavemode.modified); Configurationmanager.refreshsection ("appSettings"); }    }

Although the App. config file still cannot see the changes, the two files (Vshost.exe.config and exe.config) that were actually called when the program was run are modified so that the contents of each boot configuration file are also up-to-date. Only if your program is regenerated, two files will be overwritten by app. config content.

It's just a bit inconvenient in the modal phase, but once packaged, it's good to use, and each time you can modify the configuration file content and read the latest content.

Configuration file--app.config file read and modify

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.