Updating the configuration file and updating values ​​in the application

I am currently updating several parameters in a fairly large * .exe.config file through the * .exe executable, using XLinq to navigate directories and read / write values. The problem with updating this method is that the changes take effect only after restarting the executable file, but I want the changes to take effect instantly. Is there a way to tell the executable to reload the * .exe.config file after making the changes?

All help is appreciated and thanks in advance!

Exoskeleton for app.config

<configuration> <system.serviceModel> <!-- stuff... --> <client> <!-- this is the section I changed and want to have updated --> </client> </system.serviceModel> </configuration> 

EDIT: One of the reasons I know so little about this is that I did not create app.config - it was automatically generated by someone else's code. The reason I have to change it and make changes to the application is because another part of the code (to which I do not have access) calls the configuration file to get its data, but if I do not restart then the old settings will be used, which will not work in this application.

EDIT2: If I cannot change this dynamically, how can I change the code so that it can execute dynamically? The best answer is generosity ...

+4
source share
4 answers
 var client = System.ServiceModel.ChannelFactory<ISampleService>( System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) 

You can also connect to the service programmatically and provide WCF directly with the necessary configuration.

with this, you no longer need the wcf configuration in exe.

https://msdn.microsoft.com/en-us/library/ms576132.aspx

+1
source

Settings with a User scope can be easily saved and retrieved while the application is running. If your settings have a volume of "Application", I am afraid that you cannot change and reload them without restarting the application. Then you need to roll up your own configuration solution.

+1
source

There are two parts to doing this work. 1) Updating the correct configuration file; and 2) Forcing .net to reload changes.

1) When the .net process starts, it will copy the existing .config to the vshost.exe.config file. If you update the original configuration file after starting the process, you will not see it in the vshost.config file until you restart the process. Therefore, to do this work at run time, you need to update the vshost.exe.config file, not the exe.config file.

2) To force .net to reload the settings, you need to tell the configuration manager that the settings have been changed. You can do this with ConfigurationManager.RefreshSection ().

There are a few more examples and a couple of examples: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3c365fdb-563e-4b0a-a4b4-df684c2dd908/

+1
source

Basically, Microsoft designed it this way (reading the configuration at startup, not again), in particular, to prevent you from doing this, because the * .config file is located in the C: \ Program Files folder, and this should not be accessible for recording by a non-administrator.

+1
source

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


All Articles