开发者

Relocating app.config file to a custom path

开发者 https://www.devze.com 2022-12-13 15:22 出处:网络
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 s开发者_Go百科avin

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 s开发者_Go百科aving 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\\.


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...


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.


This worked for me.. (taken from http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)

// open config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// update appconfig file path
config.AppSettings.File = "C:\\dev\\App.config";

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload in memory of the changed section.
ConfigurationManager.RefreshSection("appSettings");

Then when you call

NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;

or any operation to fetch the app config, the new path is used.

Hope this might help someone else who has same problem!


I know this is an old question, but for those who just want to have their app.config in a different location then their binary output build location, the following works as microsoft intended it (and thus no need re-read and re-write the file to disk)

  • Define an app.config as you always do.
  • Define another config file where you want to have the actual configuration file
  • Change the app.config so that it refers to the configuration file

At runtime the settings from the configuration file will override the settings in the app.config (if present). And you are done.

Example app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
  <appSettings file="..\Config\settings.config">
    <add key="port" value="1001"/>
  </appSettings>
</configuration>

Note the file="..\Config\settings.config". You are completely free to define the path to the location where you want your users to change settings.

Example of the actual configuration file

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
  <add key="port" value="1234"/>
</appSettings>

At runtime the setting port will have the value 1234.

More info see msdn


This is an ancient question, but I ran into this same problem and came up with a hacky workaround from a few minutes in reflector:

static public class ConfigHack {
    static public void OverrideAppConfig(string path) {
        ((AppDomainSetup)
            typeof(AppDomain)
                .GetField("_FusionStore", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(AppDomain.CurrentDomain))
        .ConfigurationFile = path;
    }

    static public void ResetConfigManager() {
        typeof(ConfigurationManager)
            .GetField("s_initState", BindingFlags.Static | BindingFlags.NonPublic)
            .SetValue(null, 0);
    }
}

I've only used it on .NET2, but it looks the same in 4 in reflector. Of course I wouldn't recommend shipping this :P I only use it for quick internal things.


I am sorry if I misunderstand your request but can you not use

ConfigurationManager.OpenExeConfiguration Method (String)

Based on the change, is it possible that you can use

AppDomainSetup.ConfigurationFile Property


You could use astander's approach of calling OpenExeConfiguration. If you literally want to relocate your config file, you'll have to create your own app domain. In the process of setting up your app domain you get the chance to specify where the config file is located.

BTW, .NET config files aren't great for configuration, at least not the sort that users can modify: they're not like INI files or the registry. If you want flexibility over where your configuration comes from you're better off storing it separately.


If u use the registry, your problem can be resolved.


MSDN probably would help...

The element simplifies servicing for component assemblies. If one or more applications use an assembly that has a configuration file residing in a well-known location, the configuration files of the applications that use the assembly can use the element to include the assembly configuration file, rather than including configuration information directly. When the component assembly is serviced, updating the common configuration file provides updated configuration information to all applications that use the assembly

0

精彩评论

暂无评论...
验证码 换一张
取 消