Access to App.config in a non-binary place

In the .NET Win console application, I would like to access the App.config file in a different place than the binary console application. For example, how does C: \ bin \ Text.exe get its settings from C: \ Test.exe.config?

+4
source share
4 answers
using System.Configuration; Configuration config = ConfigurationManager.OpenExeConfiguration("C:\Test.exe"); 

Then you can access application settings, connection strings, etc. from the configuration instance. This assumes, of course, that the configuration file is properly formatted, and your application has read access to the directory. Note that the path is not "C: \ Test.exe.config" The method looks for the configuration file associated with the file you specified. If you specify "C: \ Test.exe.config", it will search for "C: \ Test.exe.config.config" Kinda lame, but it is clear, I think.

Link here: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx

+11
source

It looks like you can use the AppDomain.SetData method to achieve this. The documentation states:

You cannot insert or modify system records using this method.

Regardless, this seems to work. The documentation for the AppDomain.GetData method lists the available system records of interest as the "APP_CONFIG_FILE" record.

If we set "APP_CONFIG_FILE" before any application parameters are used, we can change where app.config loaded from. For instance:

 public class Program { public static void Main() { AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Temp\test.config"); //... } } 

I found this solution documented in this blog and a more complete answer (to the corresponding question) can be found.

+7
source

Use the following (remember to enable the System.Configuration assembly)

 ConfigurationManager.OpenExeConfiguration(exePath) 
+5
source

You can install it by creating a new application domain:

 AppDomainSetup domainSetup = new AppDomainSetup(); domainSetup.ConfigurationFile = fileLocation; AppDomain add = AppDomain.CreateDomain("myNewAppDomain", securityInfo, domainSetup); 
+2
source

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


All Articles