将app.config文件重定位到自定义路径

是否可以将整个App.Config文件重定位到自定义路径?

配置文件与exe文件位于同一文件夹中似乎有点奇怪,Windows的新approcah保存了c:\ ProgramData和all中的所有程序设置。

我们的另一个要求是以编程方式指定在哪里找到app.config文件。 这样做的原因是我们从同一个exes生成不同的服务实例,并希望将每个服务的app.config存储在c:\ ProgramData \\下该服务的settings文件夹中。

每个AppDomain都有/可以拥有自己的配置文件。 CLR主机创建的默认AppDomain使用programname.exe.config; 如果要提供自己的配置文件,请创建单独的AppDomain。 例:

// 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 = ""; // 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); 

希望有所帮助。

如果仍然相关,我们在Stack Overflow上使用了以下我在另一个问题的另一个建议答案中找到的…

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

当我们从DLL加载app.config时遇到问题时,我们工作得很好…

这对我有用..(摘自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"); 

然后当你打电话

 NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings; 

或任何获取应用程序配置的操作,都使用新路径。

希望这可以帮助其他有同样问题的人!

我知道这是一个老问题,但是对于那些只想将app.config放在不同位置然后是二进制输出构建位置的人来说,下面的工作就像微软想要的那样(因此不需要重新读取和重写)文件到磁盘)

  • 像往常一样定义app.config。
  • 定义另一个要包含实际配置文件的配置文件
  • 更改app.config以使其引用配置文件

在运行时,配置文件中的设置将覆盖app.config中的设置(如果存在)。 你完成了。

示例app.config

          

请注意file =“.. \ Config \ settings.config” 。 您可以完全自由地定义希望用户更改设置的位置的路径。

实际配置文件的示例

     

在运行时,设置port的值为1234。

更多信息,请参阅msdn

这是一个古老的问题,但是我遇到了同样的问题,并在reflection器中提出了几分钟的hacky解决方法:

 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); } } 

我只在.NET2上使用它,但在reflection器中看起来相同。 当然我不建议发送这个:PI只用它来快速内部的东西。

如果我误解了您的请求,我很抱歉,但您不能使用

ConfigurationManager.OpenExeConfiguration方法(String)

根据变化,您是否可以使用

AppDomainSetup.ConfigurationFile属性

您可以使用旁观者调用OpenExeConfiguration的方法。 如果您真的想重新定位配置文件,则必须创建自己的应用程序域。 在设置应用程序域的过程中,您可以指定配置文件的位置。

BTW,.NET配置文件不适合配置,至少不是用户可以修改的类型:它们不像INI文件或注册表。 如果您希望灵活配置来自您的配置,最好将其单独存储。

MSDN可能会有所帮助……

该元件简化了组件assembly的维护。 如果一个或多个应用程序使用具有驻留在众所周知位置的配置文件的程序集,则使用该程序集的应用程序的配置文件可以使用该元素包含程序集配置文件,而不是直接包含配置信息。 在维护组件程序集时,更新公共配置文件会向使用该程序集的所有应用程序提供更新的配置信息