Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relocating app.config file to a custom path

Is it possible to relocate the whole App.Config file to a custom path?

It seems a bit odd that the config file resides in the same folder as the exe, with Windows' new approcah of saving all program settings in c:\ProgramData and all.

An additional requirement we have is to programatically specify where to find the app.config file. The reason for this being that we spawn different service instances from the same exes, and would like to store each service's app.config in that service's settings folder under c:\ProgramData\\.

like image 521
Ries Avatar asked Dec 03 '09 08:12

Ries


People also ask

Where are app config files stored?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.


2 Answers

If still relevant, we have used the following I found on another suggested answer to another question here on Stack Overflow...

AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file") 

Worked great for us when we had issues loading app.config from DLL only...

like image 55
newby Avatar answered Sep 25 '22 23:09

newby


Each AppDomain has/can have its own configuration file. The default AppDomain created by CLR host uses programname.exe.config; if you want to provide your own configuration file, create separate AppDomain. Example:

// get the name of the assembly string exeAssembly = Assembly.GetEntryAssembly().FullName;  // setup - there you put the path to the config file AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = System.Environment.CurrentDirectory; setup.ConfigurationFile = "<path to your config file>";  // create the app domain AppDomain appDomain = AppDomain.CreateDomain("My AppDomain", null, setup);  // create proxy used to call the startup method  YourStartupClass proxy = (YourStartupClass)appDomain.CreateInstanceAndUnwrap(        exeAssembly, typeof(YourStartupClass).FullName);  // call the startup method - something like alternative main() proxy.StartupMethod();  // in the end, unload the domain AppDomain.Unload(appDomain); 

Hope that helps.

like image 22
mYsZa Avatar answered Sep 21 '22 23:09

mYsZa