如何更改 app.config 的位置 [英] How to change location of app.config
问题描述
我想更改我的应用程序查找 app.config 文件的位置.
I want to change the location where my application looks for the app.config file.
我知道我可以使用 ConfigurationManager.OpenExeConfiguration() 访问任意配置文件 - 但是,当 .Net Framework 读取配置文件(例如,对于 ConnectionStrings 或 EventSources)时,它将查看默认位置.我想实际更改整个 .Net Framework 的位置(当然,对于我的应用程序).
I know that I can use ConfigurationManager.OpenExeConfiguration() to access an arbitrary config file - however, when the .Net Framework reads the config file (for ConnectionStrings or EventSources, for instance), it will look at the default location. I want to actually change the location, globally for the entire .Net Framework (for my application, of course).
我也知道我可以使用 AppDomainSetup 来更改新 AppDomain 的 app.config 的位置.但是,这不适用于应用程序的主 AppDomain.
I also know that I can use AppDomainSetup to change the location of the app.config for a new AppDomain. However, that doesn't apply to the primary AppDomain of the application.
我也知道我可以覆盖函数 Main() 并创建一个新的 AppDomain 并在该新 AppDomain 中运行我的应用程序.但是,这有其他副作用 - 例如,Assembly.GetEntryAssembly() 将返回空引用.
I also know that I can override function Main() and create a new AppDomain as above and run my application in that new AppDomain. However, that has other side-effects - for instance, Assembly.GetEntryAssembly() will return a null reference.
考虑到 .Net 中其他所有内容的工作方式,我希望有一些方法可以配置我的应用程序的启动环境 - 通过应用程序清单或类似的 - 但我什至找不到一丝希望朝那个方向.
Given how everything else works in .Net, I would expect there to be some way to configure the startup environment of my application - via a Application Manifest, or some such - but I have been unable to find even a glimmer of hope in that direction.
任何指针都会有所帮助.
Any pointer would be helpful.
大卫·穆林
推荐答案
我使用的方法是从 Main() 启动另一个 AppDomain,指定配置文件的新"位置.
I used the approach with starting another AppDomain from Main(), specifying the "new" location of the configuration file.
GetEntryAssembly() 没有问题;它仅在从非托管代码调用时返回 null - 或者至少它不适合我,因为我使用 ExecuteAssembly() 创建/运行第二个 AppDomain,就像这样:
No issues with GetEntryAssembly(); it only returns null, when being called from unmanaged code - or at least it doesn't for me, as I use ExecuteAssembly() to create/run the second AppDomain, much like this:
int Main(string[] args)
{
string currentExecutable = Assembly.GetExecutingAssembly().Location;
bool inChild = false;
List<string> xargs = new List<string>();
foreach (string arg in xargs)
{
if (arg.Equals("-child"))
{
inChild = true;
}
/* Parse other command line arguments */
else
{
xargs.Add(arg);
}
}
if (!inChild)
{
AppDomainSetup info = new AppDomainSetup();
info.ConfigurationFile = /* Path to desired App.Config File */;
Evidence evidence = AppDomain.CurrentDomain.Evidence;
AppDomain domain = AppDomain.CreateDomain(friendlyName, evidence, info);
xargs.Add("-child"); // Prevent recursion
return domain.ExecuteAssembly(currentExecutable, evidence, xargs.ToArray());
}
// Execute actual Main-Code, we are in the child domain with the custom app.config
return 0;
}
请注意,我们正在有效地重新运行 EXE,就像 AppDomain 和不同的配置一样.另请注意,您需要有一些魔法"选项来防止这种情况无休止地发生.
Note that we are effectively rerunning the EXE, just as a AppDomain and with a different config. Also note that you need to have some "magic" option that prevents this from going on endlessly.
我从一个更大的(真实的)代码块中制作了这个,所以它可能无法按原样工作,但应该说明这个概念.
I crafted this out from a bigger (real) chunk of code, so it might not work as is, but should illustrate the concept.
这篇关于如何更改 app.config 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!