Last Updated:

.Net settings preserve

In .Net, there is a recommended mechanism for saving application settings to restore them the next time the application runs. In other words, configuration files can now be easily read by .Net tools working with them according to a single scheme. The whole rainbow picture is overshadowed by one moment, you can easily read or save any meaning if you know which interface to use at the moment. In the .Net environment, there are an obscene number of them. Without preliminary preparation, this material is not perceived in one breath and requires additional study to choose the optimal mechanism of operation. Therefore, I give a brief overview of all the tools for those who want to use it better.

A consistent narrative begins with documentation that the studio slips in regardless of the developer's desire, and then all possible methods of working with settings:


Starting to work with customizations through the project settings designer, the first thing you do, for reasons that are not clear, get to the sections of the documentation devoted to the VisualBasic language. This section appears to you regardless of the language in which you are developing and what projects are included in the current solushin. You can discount this emphasis in the technology demonstration, given that the technology for working with application settings is cross-language, but this is not the case. Getting the first information about the mechanisms of loading and retrieving values, as well as saving them from the application is very different.

From these sections of the documentation, a common architecture and mechanisms for working with settings inherent in all interfaces are formed. It becomes clear that the mechanism for working with settings from VB is standard and, apparently, was developed specifically for this environment. Thus removing all the tricky methods of working with configuration files that needed to be used before. It is from BASIC that it is convenient to add settings and remove them from the project using the designer like never before. In addition, accessing parameters from the runtime is reduced to simply accessing the My.Settings object without any additional inversions. Saving and reloading parameters is also achieved by calling My.Settings.Save(), My.Settings.Reload(). In general, no problem. But the full picture of working with parameters becomes really detailed if you refer to the documentation for C# in the Settings section.

C #

Keep in mind that settings are divided into two areas: user and application. Each area has its own specific features reflecting the mechanisms of working with them. In addition, the settings included in the application from the configuration files are not read directly from the app.config located in the directory with the program, but are additionally merged with the user's configuration files. These user-specific files are stored in %APPDATA%\[company]\[product]\[version]\user.config, as well as in Local Settings\Application Data\[company]\[product]\[version]\user.config.

Variables from C# are accessed through namespace Properties. Reading and setting a variable is done by accessing the Properties.Settings.Default.myName value. Write the changed settings by calling the Save() method on the Properties.Settings.Default.Save() object.  


All the confusion begins when you create a WebService project and try to set parameters in the usual way for you. Working through the settings designer, the changes you create go into the Settings.settings files of the project, instead of the web.config. But this is not such a big problem compared to the large selection and confusing schemes for working with the settings offered for work. Here are some brief remarks:

Firstly, using the web service, the studio with all its might wants to offer you to work according to the old scheme and it is good at it. You can easily save the settings where they should not be, as well as easily get their values. Alternatively, you can create a simple application that works according to the rules of local forms and connect it to the main Web application. Thus achieving poor results.

Secondly, realizing that the settings must be saved in the web.config file, you first of all view its contents and instantly get lost among the many configuration sections.

And finally, thirdly: the documentation presents various methods of working with the settings of web services, some of which are already recommended not to use.

Other sections on this issue are simply marked as not safe and are only suitable for familiarization with the technology.


The settings in the web.config file are described in the ASP.NET Configuration Settings document.

An important feature of the work of configuration files for the web is an advanced, in comparison with standard applications, mechanism for inheriting settings. The main configuration file is contained in the systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\Web.config folder. All values of this file can be overwritten by the new configuration file located in the working folder. At the same time, all files located in Working Folders can also be overwritten by configuration files located at a level lower in child folders.

By default, the created configuration file in the new WebService project contains little information. I couldn't find any documentation for this section, but if you need to know all the possible settings and purpose of each partition, you'll need to review the main configuration file. It is located in the systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\* folder. For example, in order to find out what the mysterious section is. You need to open machine.config, find out the type of the partition of the same name in the type="System.Configuration.AppSettingsSection" attribute, and then you can open the documentation section for a particular topic. Thus, running through all the significant fields, we get an idea of the rules for working with the application settings for the web.


As for working with WebServices, I will only say that you need to use the WebConfigurationManager class. The following link can see an example of the work. It also states that this is the preferred way to work with settings for web applications, but for client programs that communicate with this service, you must select the ConfigurationManager class.  


Another method of working with configuration files appeared here at the same time. Do not use it when working under .Net  


Saving settings to the registry is another way to save application settings between restarts. However, this method is a mechanism of inheritance.


File store

Saving data to disk with subsequent use, it is recommended to do this path:


Team Work

One issue I left behind is working together on a project. No matter how it may seem at first glance, this is strongly related to the topic at hand and primarily affects the use of general personalization settings for each developer machine.

We use various version control systems that ensure the synchronization of code between developers. As a result, we have absolutely identical copies of the project on all machines. At the same time, we need to provide individual settings and lines of connection to the database, for the specific environment in which the programmer works with his authentication data. This can be done in the old-fashioned way - changing the contents of project files for specific needs. However, it is better to use configurations managed through the ConfiguarationManager (or its web version).

To do this, we will create a common configuration template and place it under the version control system. After that, we will create a second working copy, which will be distributed past the central project repository via e-mail.

To transparently connect this file to the project, we will use the inheritance mechanism offered by default. Since ASP does not offer a flexible inheritance scheme we will have to exclude the Web.config file from the file repository renaming it to Web.default.