0

In my application I start a process through my code with app.config using following code

Process proc = Process.Start(@"c:\windows\system32\notepad.exe");

Than I have to change app.config file two or three times while running it. I am wondering how to do it so I dont need to stop and change the app.config file and than re start the process.

I have following code that I saw in another post.

// Load the app.config file
XmlDocument xml = new XmlDocument();
xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

// Do whatever you need, like modifying the appSettings section

// Save the new setting
xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

I am wondering if last line where it saves the new setting that is being done while the process is still running? or process needs to be re started? My goal is where I dont need to keep restarting the process. Thanks

J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • Please explain why you need to change your `app.config`. – Daniel Hilgarth Feb 05 '13 at 15:04
  • I have some wcf settings that needs to be updated while application is running in the config.app – J. Davidson Feb 05 '13 at 15:08
  • 1
    I am not sure this is the best approach. But it is possible: http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime/6151688#6151688 – Daniel Hilgarth Feb 05 '13 at 15:10
  • I have done this before Dawood I too the XPATH approach you may want to do a google search on how to update .Config file using XPATH good luck – MethodMan Feb 05 '13 at 15:13
  • 1
    Take a look at [ConfigurationManager.RefreshSection](http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx) – Metro Smurf Feb 05 '13 at 15:19

2 Answers2

0

So in WCF, the intent of the app.config is to provide administrators the ability to make changes to the service without having to recompile the code (something they often don't know how, or have the tools to do). If you're writing the code, there is no reason you should need to change the app.config file because you are the program author.

Remember, if you're just trying to re-host at a different address (binding or what have you), then just close down the service and restart it in code, a la:

WCF Self Host Service - Endpoints in C#

Hope this helps

Community
  • 1
  • 1
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
0

If you don't want to restart the child process, you need to use an inter-process communication (IPC) technique to notify the child process that it needs to refresh it's app.config. How you do this depends somewhat on the scale of your application and how much use you're going to get out of the IPC link.

see this SO post for debate on the simplest to use IPC in C#. and this post for the code you'll need to call in your child process to update it's app.config

EDIT:

I found a better post explaining additional options for inter-process communication.

Community
  • 1
  • 1
Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46