如何改变的app.config的位置 [英] How to change location of app.config

查看:398
本文介绍了如何改变的app.config的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变在我的应用程序查找app.config文件的位置。

I want to change the location where my application looks for the app.config file.

我知道,我可以使用ConfigurationManager.OpenExeConfiguration()来访问任意配置文件 - 然而,在.Net框架读取配置文件(的ConnectionStrings或EventSources,例如),它会查找在默认位置。我想真正改变的位置,在全球整个.Net框架(我的应用程序,当然)。

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改变在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.

任何指针将是有益的。

大卫·穆林

推荐答案

我使用的方法与开始从主()另一个AppDomain中,指定配置文件中的新的位置。

I used the approach with starting another AppDomain from Main(), specifying the "new" location of the configuration file.

与GetEntryAssembly没有问题的();它只返回空值,当被称为从非托管code - 或者至少是不适合我,因为我用ExecuteAssembly()来创建/运行第二个应用程序域,就像这样:

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中,并用不同的配置。另外请注意,你需要有一些神奇的选项,起价无休止地去页上$​​ pvents这一点。

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.

我制作了这一点,从code更大的(真正的)块,所以它可能无法正常工作原样,但应说明这一概念。

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屋!

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