c # configuration file app.config and Settings.settings

An example of a desktop program first introduced in when we install a new software, for example, will have to guide novice software starts and other forms pop up, each requires its own to close it. Of course, the software will provide, for example no longer display. When you select function is no longer displayed, these forms will no shows.

With these settings, the following methods can be used to store custom XML file, registry, of course, also be a built-in C # Settings folder. Recommended Settings file. Do not use the registry easily, need custom XML parsing in use, when possible alternatives.

Below an example to show through,

Create a new WPF project,

Blue background is Settings,

Setting a has four attributes Name, Type, Scope, Value. Scope highlight some attributes, Scope property has two values ​​Application, User. This distinction between the two, when the value of Scope Application, corresponding Setting can not be modified at runtime. When the value Scope User, corresponding Setting can be modified at runtime.

Setting Saved in: When the program is compiled, the value stored in the Setting .exe.config file, for example:

Used in the program Setting,

        // Modify the setting value
        Properties.Settings.Default.DisplayGuide = false;
        // Save setting value
        Properties.Settings.Default.Save();

Wherein, in the Attribute Settings The range is divided into two "user" and "applications" (wherein: Application-level configuration is read, and the user level is read-write.)

The range of "application" of the property, are obtained from APP.CONFIG read, the settings can also be changed manually modify the App.config, but in the process it can not be assigned, can only be read.

The range of "users" of the property in the App.config Settings reads the initial value in the first run

But once call the Save method, Settings in the range of "user" property will be stored inside the system, similar to the same WebForm in the Cookies

Since then, the reading will be saved from the system's value in reading, manually modify the App.config in the attribute "user" will not affect the scope of these properties, but it will get from the new App.config inside when you call the Reset method "user" is written into the scope of the property system.

        也就是说,settings里“用户”范围的属性总共有3个地方存储。

1, app.config configuration file ----- program is first run, before the program does not call save (), it will not be found in the system is available here

2, when the operating system settings -------- call save () to save.

3, ---- memory after the program starts, settings of an object instance, the instance is created at the time of reading from the memory in the system (if not, as previously mentioned system obtains from the profile).

Once we add value to it by VC #, in the project directory will generate a app.config file.

So, these two things what is the relationship?

Add two string type of configuration values ​​(an application level, a user-level) after:

Settings.settings





dbzhang800


1+1=2


app.config











<WpfApplication1.Properties.Settings>

1+1=2

</WpfApplication1.Properties.Settings>


<WpfApplication1.Properties.Settings>

dbzhang800

</WpfApplication1.Properties.Settings>

Though all the xml file, file two different styles, which is what we app.config before one introduced to the Configuration file from its configSections can be seen that it has two custom segments, and are called applicationSettings userSettings

Operation of such a file, we can create ConfigurationManager namespace ConfigurationSection derived class, and then follow through the class.

Settings.settings seems quite simple, but app.config and compared it contains configuration values ​​of the type (for example: Type = "System.String").

And, whether we modify Settings.settings or app.config, Visual studio will prompt us, and automatically synchronized to another file.

Settings.settings 文件的存在的目的是为了生成一个名为 Settings.Designer.cs 的文件,该文件内的类提供了操作配置文件 (比如:program.exe.config) 的封装类。
app.config 会被拷贝到应用程序目录下,并变身为 program.exe.config

Thus, in the program can

        Properties.Settings.Default.thisIsUserSettings = "999999999";
        console.WriteLine(Properties.Settings.Default.thisIsAppSettings);
        Properties.Settings.Default.Save();

To manipulate the configuration files.

User configuration will be written to

C: \ Users \ Administrator \ AppData \ Local "Project Name" \ WPFUserSetting.exe_Url_v4wjaifsqnskfnd0x5zhrlngptsdqoii \ 1.0.0.0 \ user.config

These files.
.Settings multiple files

With a project can be added .settings plurality of files, each will produce a package type, part of each of the app.config operation.

Guess you like

Origin www.cnblogs.com/caozhengze/p/11275068.html