Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Config File through C # Program

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to operate Config files through the C # program", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to operate Config files through C# procedures" can help you solve your doubts.

For config files, you usually use ConfigurationManager to load and then read the values of the corresponding nodes to get the desired data, but sometimes you need to modify the values of the config file, so you use the OpenExeConfiguration () method.

MSDN's explanation of this method above: the ConfigurationManager.OpenExeConfiguration method is used to open the specified client configuration file as a Configuration object, which has two overloads:

The name indicates that ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel) opens the configuration file of the current application as a Configuration object. ConfigurationManager.OpenExeConfiguration (String) opens the specified client profile as a Configuration object. Use OpenExeConfiguration (ConfigurationUserLevel) overload to set the configuration file of the current application

Client applications use global configurations that apply to all users, separate configurations that apply to individual users, and configurations that apply to roaming users. The userLevel parameter determines the location of the open profile by indicating whether the profile does not have a user level (the profile is in the same directory as the application) or has a user level on a per-user basis (the profile is in the application settings path determined by the user level).

Specify the configuration to get by passing one of the following values to userLevel:

To get the Configuration object that applies to all users, set userLevel to None.

To get the local Configuration object applied to the current user, set userLevel to PerUserRoamingAndLocal.

To get the roaming Configuration object applied to the current user, set userLevel to PerUserRoaming.

Note: to get the Configuration object of a resource, your code must have read privileges on all configuration files from which it inherits settings. To update a configuration file, your code must also have write privileges on the configuration file and its directory.

Sample program:

1. The configuration file structure is as follows: 2. Modify the value of LocalHost1 node string strLocalHost1Value1= ConfigurationManager.AppSettings ["LocalHost1"] .ToString (); / / strLocalHost1Value1= "LocalHost1"; / / Configuration object Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None); config.AppSettings.Settings ["LocalHost1"] .Value = "http://127.0.0.1";// save configuration file config.AppSettings.SectionInformation.ForceSave = true Config.Save (ConfigurationSaveMode.Modified); / / reload the changed node ConfigurationManager.RefreshSection ("appSettings"); / / read the value of the configuration file string strLocalHost1Value2= ConfigurationManager.AppSettings ["LocalHost1"] .ToString (); / / strLocalHost1Value2= "http://127.0.0.1" II. Use OpenExeConfiguration (String) to reload the specified client configuration file

Reloading the specified client config file mainly includes the following three situations:

1. Load the xxx.exe.config file that is not the default config file of the current application yyy.exe (yyy.exe is the current application, xxx.exe.config and yyy.exe.config files are not in the same directory).

2. Load non-application xxx.config files.

3. When the function in the class library xxx.dll reads the default config file, it reads the xxx.dll.config file under the same level directory of xxx.dll, instead of loading the default application configuration file of yyy.exe of xxx.dll: yyy.exe.config.

Note: using ConfigruationManager in the class library does not read the automatically compiled xxx.dll.config file, but the yyy.exe.config file of the application yyy.exe that references the class library.

Solution:

According to the instructions on MSDN, we pass in the path of the xxx.exe.config to be opened as a parameter. The code is as follows:

Configuration con = ConfigurationManager.OpenExeConfiguration ("C:\\ Modify.exe.config"); con.AppSettings.Settings ["LocalHost2"] .Value = "Test"

However, the program reported an error when it was running. after debugging, it was found that the value of the FilePath property of the con object was: C:\ Modify.exe.config.config. The program itself added ".config" as the path to the config file to be opened after the passed parameters, because there was no such file, so the program reported an error. The parameter to be passed in here should not be the path of the config file to be opened, but the path of the application corresponding to the config file. The above code should be changed to:

/ / the parameter passes the application path Configuration con = ConfigurationManager.OpenExeConfiguration ("C:\\ Modify.exe."); con.AppSettings.Settings ["LocalHost2"] .Value = "Test"

Run the program again, or report an error, indicating that "error loading configuration file: parameter exePath" is invalid. It's right to pass in the exePath of the application here, but because there is no xxx.exe file in the same directory of the xxx.exe.config file, the exePath we passed in is actually invalid. In order to be able to load the xxx.exe.config file, we need to add a xxx.exe file in the same directory. (you can create a new txt file in the same directory and change the name to xxx with a .exe extension so that the xxx.exe.config configuration file can be loaded.)

The complete code is as follows:

/ / pass the path of the application Configuration con = ConfigurationManager.OpenExeConfiguration ("C:\\ Modify.exe"); con.AppSettings.Settings ["LocalHost2"] .Value = "Test"; / / Save the configuration file con.AppSettings.SectionInformation.ForceSave = true;con.Save (ConfigurationSaveMode.Modified); / / reload the changed node ConfigurationManager.RefreshSection ("appSettings"); / / read the modified configuration file node value string str = con.AppSettings.Settings ["LocalHost2"] .Value / / str= "Test"

Note:

Just use ConfigurationManager.OpenExeConfiguration (string exePath), and pay attention to 2 small details:

A: exePath, not configPath, should be passed in to change the method.

B:exePath must be valid, so xxx.exe and xxx.exe.config should appear in pairs, both of which are indispensable.

Load xxx.config files that are not applications

In the above example, looking at the name of the xxx.exe.config file, it is found that if you think of xxx.exe as YYY, then xxx.exe.config=YYY.config, that is, xxx.exe.config is a special form of xxx.config file, so you can load the xx.config file with the following code:

/ / pass the path of the application Configuration con = ConfigurationManager.OpenExeConfiguration ("C:\\ Modify"); con.AppSettings.Settings ["LocalHost2"] .Value = "Test"; / / Save the configuration file con.AppSettings.SectionInformation.ForceSave = true;con.Save (ConfigurationSaveMode.Modified); / / reload the changed node ConfigurationManager.RefreshSection ("appSettings"); / / read the modified configuration file node value string str = con.AppSettings.Settings ["LocalHost2"] .Value / / str= "Test"

Note: C:\ Modify this file must have.

Load the xxx.dll.config file:

Or from the file name to find the idea, we want to load the xxx.dll.config file, can be the same as loading the xxx.config file. In dll, when you encounter the need to read config file information, instead of using ConfigurationManager to read the value of the node, use the OpenExeConfiguration (string exePath) method to load the config file as a Configuration object to use.

Note: modifying the value of the node in the configuration file through the program will not modify the value in the .config file, the change will only occur in memory.

Read here, this article "how to operate Config files through the C # program" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report