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

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

问题描述

是否有可能对整个app.config文件重新定位到自定义路径?

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

这似乎有点奇怪,该配置文件驻留在同一文件夹中的exe文件,以节省C所有程序设置的Windows的新approcah:\ 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文件。在C这样做的原因是,我们从产卵同前男友不同的服务实例,并希望存储每个服务的的app.config在该服务中的设置文件夹:\ 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);

希望有所帮助。

Hope that helps.

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

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