5

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

Chris Comeaux
  • 55
  • 1
  • 1
  • 6

5 Answers5

13
using System.Configuration;    

Configuration config =
ConfigurationManager.OpenExeConfiguration("C:\Test.exe");

You can then access the app settings, connection strings, etc from the config instance. This assumes of course that the config file is properly formatted and your app has read access to the directory. Notice the path is not "C:\Test.exe.config" The method looks for a config file associated with the file you specify. If you specify "C:\Test.exe.config" it will look for "C:\Test.exe.config.config" Kinda lame, but understandable, I guess.

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

jeff.willis
  • 346
  • 1
  • 5
  • 1
    One gotcha with this method is that the file "C:\Test.exe" must exist, even if it's not a valid executable. – Chris Comeaux Oct 06 '08 at 14:47
  • 1
    What about any thing else that assumes you have your connection string in your app.config is your bin ? like EntityFramework which automatically retrieves your connection string from you app.config . The question here is how can i tell the configuration manager look for my config in a location. not create an instance from some location and pass it around – eran otzap Dec 14 '16 at 10:20
8

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

You cannot insert or modify system entries with this method.

Regardless, doing so does appear to work. The documentation for the AppDomain.GetData method lists the system entries available, of interest is the "APP_CONFIG_FILE" entry.

If we set the "APP_CONFIG_FILE" before any application settings are used, we can modify where the app.config is loaded from. For example:

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 a related question) can be found here.

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
6

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

ConfigurationManager.OpenExeConfiguration(exePath)
Santiago Palladino
  • 3,522
  • 2
  • 26
  • 36
3

You can set it by creating a new app domain:

AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ConfigurationFile = fileLocation;
AppDomain add = AppDomain.CreateDomain("myNewAppDomain", securityInfo, domainSetup);
Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
0
AppDomainSetup domainSetup = new AppDomainSetup();
        domainSetup.ConfigurationFile = @"D:\Mine\Company\";
        string browserName = ConfigurationManager.AppSettings["browser"];