4

I am currently updating a few settings in a rather large *.exe.config file via the *.exe executable by using XLinq to navigate the directories and read / write the values. The problem with updating this way is that changes only take effect after restarting the executable, but I would like the changes to take effect instantaneously. Is there a way to tell the executable to reload the *.exe.config file after I make 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 that I know so little on this subject is that I didn't create the app.config - it's auto-generated by somebody else's code. The reason that I have to change it and have the changes take effect in the application is that another part of the code (which I have no access to) calls on the config file to get its data, but if I don't reload the section then the old settings will be used, which won't work in this application.

EDIT2: If I can't change this dynamically, how do I change the code so that it can be done dynamically? Best answer gets the bounty...

adam_0
  • 6,920
  • 6
  • 40
  • 52
  • possible duplicate of [.net dynamically refresh app.config](http://stackoverflow.com/questions/272097/net-dynamically-refresh-app-config) – Blair Conrad Jul 14 '10 at 21:10
  • None of those solutions work for me. I'll post some of the pseudo of my app.config file to aid the process. – adam_0 Jul 14 '10 at 21:24
  • Oh dear oh dear. Out of all possible bits of config the app could be using, you want to change WCF endpoints while calls are in progress. The app needs some *serious* re-writing if it wants to construct WCF endpoints dynamically. – Christian Hayter Jul 20 '10 at 17:25
  • That's what I figured would have to happen, but I'd rather not mess with that code if possible, since it's been maintained by a different person. Is there no way to change this data from within the application? – adam_0 Jul 20 '10 at 17:45

4 Answers4

1

Settings with scope "User" can be easily stored and retrieved while the Application is running. If your settings are of scope "Application" I'm afraid you cannot modify and reload them without restarting your application. You'll need to roll your own configuration solution then.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • How can I figure out the scope of my settings? – adam_0 Jul 16 '10 at 23:34
  • 2
    If it's in the application.exe.config file, then it's application-scoped. If the config file is in your user profile/AppData/Local folder, then it is user-scoped. And judging from what I see in your code snippet, you're not going to be able to reload dynamically. .NET Configuration just can't do that at this time. You may have luck using configSource attribute. – Eric Falsken Jul 21 '10 at 04:50
  • @Eric: Exactly. This issue is well-known and documented appropriately on MSDN. – Johannes Rudolph Jul 21 '10 at 08:00
  • What's the configSource attribute? Also, if I were to put these configurations in a new file, how would I read/write to them? Best answer gets the bounty... – adam_0 Jul 21 '10 at 16:30
1

There are 2 parts to making this work. 1) Updating the correct config file, and 2) forcing .net to reload the changes.

1) When a .net process starts, it will copy the existing .config to the vshost.exe.config file. If you update the original config file after the process has started, you will not see it in the vshost.config until you restart the process. So to make this work at runtime, you need to update the vshost.exe.config, not the exe.config file.

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

There is some more information and a couple code examples at: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3c365fdb-563e-4b0a-a4b4-df684c2dd908/

Mike
  • 3,462
  • 22
  • 25
  • I'm pretty sure what you said in 1) is wrong. The *.vshost.exe executable is only used when running a program in the Visual Studio debugger. Before debugging an app, VisualStudio copies the app.config to myapp.exe.config & myapp.vshost.exe.config – James Curran Jul 26 '10 at 19:22
  • This method works the first time I change the file, but subsequent changes don't take effect. Any idea why this is happening? – adam_0 Jul 26 '10 at 20:42
  • This isn't the "correct" answer yet, as I haven't gotten it to work properly, but it was the closest so I awarded the bounty here. – adam_0 Jul 27 '10 at 15:24
1

Basically, Microsoft designed it this way (having the configuration read at start up and not again), specifically to discourage you from trying this, because the *.config file live in the C:\Program Files folder, and that should not be writable by a non-Administrator.

James Curran
  • 101,701
  • 37
  • 181
  • 258
1
var client = 
 System.ServiceModel.ChannelFactory<ISampleService>(
  System.ServiceModel.Channels.Binding binding, 
  System.ServiceModel.EndpointAddress remoteAddress)

you can connect to a service also programmatically, and give the WCF directly the config needed.

with using this, you do not need the wcf config in the exe any more.

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

tom redfern
  • 30,562
  • 14
  • 91
  • 126
cRichter
  • 1,411
  • 7
  • 7
  • Link worked great when I switched it to en-us instead of de-de :) I will see if I can get a look at the code that makes the client and try to implement your suggestion. – adam_0 Jul 27 '10 at 15:35