将 app.config 文件重定位到自定义路径 [英] Relocating app.config file to a custom path

查看:29
本文介绍了将 app.config 文件重定位到自定义路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to relocate the whole App.Config file to a custom path?

配置文件与 exe 位于同一文件夹中似乎有点奇怪,而 Windows 的新方法是将所有程序设置保存在 c:\ProgramData 等中.

It seems a bit odd that the config file resides in the same folder as the exe, with Windows' new approcah of saving all program settings in c:\ProgramData and all.

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

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

推荐答案

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

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

希望有所帮助.

这篇关于将 app.config 文件重定位到自定义路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆