Assume that your application configuration file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "name" value = "old"/>
</AppSettings>
</Configuration>
OK. How can I modify the name value at runtime ??
There are many children's shoes that can use Xml to read the configuration file, and then xxx ....
Of course, this method can certainly solve the problem. Is there any other way ??
Here I want to introduce a simple method, which may already be known, that is, using the ConfigurationManager class.
ConfigurationManager exists in System. Configuration. dll.
The code is as follows:
Public static void Main () {Console. writeLine (ConfigurationManager. appSettings ["name"]); ChangeConfiguration (); Console. writeLine (ConfigurationManager. appSettings ["name"]); Console. readLine ();} private static void ChangeConfiguration () {// read the Assembly configuration file string assemblyConfigFile = Assembly. getEntryAssembly (). location; Configuration config = ConfigurationManager. openExeConfiguration (assemblyConfigFile); // you can call this operation to obtain the appSettings node paiettingssection paiettings = (AppSettingsSection) config. getSection ("deleetask"); // delete the name, and then add the new value of deleetask. settings. remove ("name"); deleettings. settings. add ("name", "new"); // Save the config file. save ();}
The code is very simple: first read the configuration file, then get the deleteworker node, modify it, and save it.
Run: The result is as follows:
The output value is two old ones.
Why ??
You can find the msdn document and find that Microsoft uses a cache policy for ConfigurationManager for performance considerations. Therefore, if you want to read new values,Should be usedRefresh section of ConfigurationManager,
ConfigurationManager. RefreshSection:
Refresh the naming section so that it will be re-read from the disk the next time it is retrieved.
Modify the Main method:
Console. WriteLine (ConfigurationManager. Deleetpipeline ["name"]);
ChangeConfiguration ();
ConfigurationManager. RefreshSection ("receivettings ");
Console. WriteLine (ConfigurationManager. Deleetpipeline ["name"]);
Clear the solution and run it again:
We can see that there are still two old ones...
Why ??
Didn't the value be modified ??,Open the application configuration file and obtain the path by monitoring assemblyConfigFile.
The parameter is xxxbindebugcastudy.exe.corresponding to the configuration file castudy.exe. config.
The file content is as follows:
We can see that the value has been changed. Why is the output old or old ??
To verify that it is not a problem with VS2010.
First, change the value of the castudy.exe. config file to "clientold.exe", and run castudy.exe again. The result is as follows:
The output values are old, and new. Why ???
Isn't the modified configuration file read during debugging, or is the modified configuration file not the file read by the Debugging application ??
When you set a breakpoint in assemblyConfigFile, you can find that assemblyConfigFile reads castudy.exe. Config. However, when vscompute is running, it is castudy.vshost.exe. That is to say, we use the ConfigurationManager. OpenExeConfiguration workflow. config file.
Is there any other way to obtain the application configuration file accurately ??
Yes, use AppDomain. CurrentDomain. SetupInformation. ConfigurationFile;
Modify the ChangeConfiguration () method as follows:
Private static void ChangeConfiguration () {// read the Assembly configuration file string assemblyConfigFile = Assembly. getEntryAssembly (). location; string appDomainConfigFile = AppDomain. currentDomain. setupInformation. configurationFile; Configuration config = ConfigurationManager. openExeConfiguration (ConfigurationUserLevel. none); // Obtain the ettings node paiettingssection paiettings = (AppSettingsSection) config. getSection ("deleetask"); // delete the name, and then add the new value of deleetask. settings. remove ("name"); deleettings. settings. add ("name", "new"); // Save the config file. save ();}
Clear and run again:
Use the default version without passing strings to open the current configuration file.
Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
To view the complete path of the current configuration file, use AppDomain. CurrentDomain. SetupInformation. ConfigurationFile;
Run the command again. The result is as follows:
In addition, ConfigurationManager. RefreshSection does not support section Group. Therefore, for WCF services, you must RefreshSection one by one:
ConfigurationManager. RefreshSection ("system. serviceModel/behaviors ");
ConfigurationManager. RefreshSection ("system. serviceModel/bindings ");
ConfigurationManager. RefreshSection ("system. serviceModel/client ");
ConfigurationManager. RefreshSection ("system. serviceModel/services ");