是否有可能设置程序集探测路径W / O型的app.config?有可能、路径、程序、config

2023-09-03 06:03:18 作者:△爱在老地方

我需要把dll的我内子文件夹的应用程序。它可以设置经由的app.config该子文件夹:

I need to place DLLs for my application inside subfolder. It is possible to set this subfolder via app.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="Libs"/>
    </assemblyBinding>
  </runtime>

但对于一些原因,我不想使用.config文件在这种情况下。是否有可能设置直接从应用程序code探测路径?我相信,DLL的永远是这个文件夹中。

But for some reasons I don't want to use .config file in this case. Is it possible to set probing path directly from application code? I am sure that DLLs always be within this folder.

任何想法?

推荐答案

的探测路径由AppDomainSetup用于主应用程序的域定义。在默认的CLR主机,即公元得到之前,你的code开始运行自动创建。配置其设置的唯一方法是使用一个.config文件,它必须具有相同的名称作为exe文件。在这之后是冰冻的,你在你的code所做的任何更改将不会有效果。

The probing path is defined by the AppDomainSetup for the primary app domain. In the default CLR host, that AD gets automatically created before your code starts running. The only way to configure its setup is to use a .config file, it must have the same name as the exe. After which it is frozen, any changes you make in your code won't have an effect.

解决方法是建立自己的广告,因此您可以更改其设置或执行AppDomain.AssemblyResolve事件。两者都不逊色于简单的解决方案:一个config文件或者只是保持装配在正确的目录。情况因人而异。

Workarounds are to create your own AD so you can change its setup or implementing the AppDomain.AssemblyResolve event. Neither of which compares favorably to the simple solutions: a .config file or just keeping the assembly in the right directory. Ymmv.