Relocating App.Config File to a Custom Path

Relocating app.config file to a custom path

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.

Moving app.config file to a custom location

Take a look at this link:

How to read app.config from a custom location, i.e. from a database in .NET

this answer in particular:

What about using:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath)
That should let you open an arbitrary app.config file.

Relocating app.config to a custom path for WCF?

Thanks to WCF Client without app.config/web.config by Kishore Gorjala, I eliminated all reliance on an app.config as follows:

EndpointAddress endpointAddress = new EndpointAddress("http://myServiceURL.com");
WSHttpBinding serviceBinding = new WSHttpBinding();
serviceBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
MyServiceClient myClient = new MyServiceClient(serviceBinding, endpointAddress);

According to this blog, you might want to try BasicHttpBinding instead of the WSHttpBinding as well.

This technique is also mentioned on the blog Minimal WCF server/client WITHOUT app.config.

Experimental evidence: This worked perfectly - and no more app.exe.config to worry about.

C# Move AppSettings from .exe.config in Program Folder to Custom Location

You can do it. Read the config file like this:

Configuration App_Config = 
ConfigurationManager.OpenExeConfiguration("C:\Temp\PROGRAMNAME.exe");

By this, it will look for the config file PROGRAMNAME.exe.config in the specified path C:\Temp

Please notice the path is not specified as C:\Temp\PROGRAMNAME.exe.config

Get more info from Here

App.config for different environments folder structure


<ItemGroup>
<Compile Update="App.*.config">
<DependentUpon>App.config</DependentUpon>
</Compile>
</ItemGroup>

Add/append above lines to your .csproj file should do the trick. (Assuming your project is using .net core)



Related Topics



Leave a reply



Submit