Someone asked me about the reading of Additional configuration files on my blog two days ago. Unfortunately, I wrote this blog one year ago, in addition, I haven't tried programming for another year. I tried it for one night in the local msdn library when the network was disconnected. No results were found. I searched the internet this morning, I found someone encountered such a problem and finally found the misleading error of msdn. I 'd like to share with you.
Bytes ----------------------------------------------------------------------------------------------
First, let's talk about configrationmanager. openexeconfiguration (exepath:
1. this is a bug. exepath is the path of the configuration file to be read in the description of msdn. configuration. configuration A = system. configuration. configurationmanager. in openexeconfiguration (exepath), you can see its filepath attribute in the generated object A, which is surprising. The displayed path is the original exepath path +. config.
So the object is always blank, because your X. config exists, but it reads X. config. config.
Is this a false understanding, not a bug?
No, enter in exepath. after the previous part of config, an error is prompted, and the configuration file cannot be read, that is, its configurationmanager. the openexeconfiguration () method requires that the exepath must be one. config File !!!
2. possible causes of this bug
I guess its original reading methods and classes are all in the form of a configuration file: Executable File Name: cmd.exe +. config format, or machine. config. at this time, no identification is required. in. NET 2.0, the method added to read the Additional configuration file, due to some errors, is still matching the original old method, resulting in it automatically added in the read. config.
Bytes ---------------------------------------------------------------------------------------------
Solution
I have referred to the <configuration class in the web page Implementation of Web. config modification> (http://tech.sina.com.cn/s/2008-06-30/1013712947.shtml) Method
That is, the method of true and false config files:
1. Create an additional config file in the bin/DEBUG directory of the project, that is, X. config;
2. Create an X. config. config file in the same directory;
3. All information is stored in X. config. config, that is, the deleteworker node;
4. Read and Write Data.
Bytes -----------------------------------------------------------------------------------------------
I wrote a small example:
1. Code for reading the configuration file information // path of the configuration file, which is the path of the configuration file (false)
String Path = application. startuppath + "\ app2.1.config ";
// Display the result, that is, the value of the machineid field of the configuration file
String showcontent = "";
// Object A is produced from an additional configuration file (true), while exepath, that is, path is the path of the configuration file (false), and the configuration file (true) path = path of the configuration file (false) +. config; and the configuration file name (false) should also be. the end of config.
System. configuration. Configuration A = system. configuration. configurationmanager. openexeconfiguration (PATH );
// If the configuration file does not include the ettings section, an exception is thrown.
If (A. Sections ["etettings"] = NULL)
{
Throw new exception ("the configuration file does not contain the deleteworker node ");
}
Else
{
Showcontent = A. deleteworkflow. settings ["machineid"]. value. tostring ();
}
// The label displays the machineid value in the configuration file.
Label1.text = showcontent;
2. Configuration File Name: app2.1.config. config
The content is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "machineid" value = "app2.1 long"/>
</Appsettings>
</Configuration>
3. The final read result is displayed in the label, app2.1 long
In fact, this is a helpless method.
--------------------------------------------------------------------
Reference: 1. <configuration class modifies Web. config on a webpage> http://tech.sina.com.cn/s/2008-06-30/1013712947.shtml;
2. <depressing configurationmanager> http://www.cnblogs.com/drizzlecrj/archive/2007/04/04/697146.html;
3. http://msdn.microsoft.com/zh-cn/ms224437 (vs.80). aspx;