Is it possible to serialize changes in the configuration of your application to the app.config application file?

Is there a built-in mechanism for serializing changes to the app.config file using the built-in functions available in .NET?

For example, if I have a custom property set in Executable.exe.config that changes at runtime, I would like .NET to update Executable.exe.config accordingly.

I know that I can do this by creating my own serialization mechanism, but I would like to know if it is possible to use functions that are already available in .NET by default.

+4
source share
1 answer

You can use the ConfigurationManager class.

There are many examples from the link above or in this article , which also contains a brief example. You need to add the link before System.Configuration , and basically you access and save the file as follows:

 // Open App.Config of executable System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Do stuff // Save the configuration file. config.Save(ConfigurationSaveMode.Modified); 

To force your program to update data immediately, you need to update the section using the ConfigurationManager.RefreshSection method

 // Force a reload of a changed section. ConfigurationManager.RefreshSection("appSettings"); 

And it is important to note this thread , for those who say that it does not work in debug mode:

If you use the code from the debugger (in VS) than your code really changes YourAssemblyName.vshost.exe.Config If you run YourAssemblyName.exe directly from the bin \ debug folder. YourAssemblyName.exe.Config will change

+3
source

Source: https://habr.com/ru/post/1486703/


All Articles