App.config运行时使用通配符或递归进行探测

时间:2015-10-19 09:49:40

标签: c# app-config

我正在尝试通过<probing>元素加载dll。我有一个文件夹结构,包含插件内的几个插件文件夹。所以我正在寻找一种方法来递归遍历所有这些插件文件夹来查找dll。

这是我的文件夹结构:

  • 所有MyApplication
    • MyApp.exe的
  • 插件
    • fooplugin
      • foo.dll
    • barplugin
      • bar.dll

这就是我的App.config看起来像,但它似乎没有抓住plugins\*部分。

<?xml version="1.0" encoding="utf-8">
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="MyApplication;plugins;plugins\*;"/>
    </assemblyBinding>
  </runtime>
</configuration>

如果我这样做:<probing privatePath="MyApplication;plugins;plugins\fooplugin;">它会找到foo.dll。但我不确定哪些插件会出现。

2 个答案:

答案 0 :(得分:1)

使用probing元素时无法使用通配符搜索。相反,您需要将DLL复制到共享文件夹中或指定它们可能存在的每个文件夹。

如果你的插件是由其他项目生成的,你可以将它们设置为直接构建到你的插件文件夹,或者最好在MyApplication项目中有一个post build任务,将文件复制到你的项目插件文件夹中。

答案 1 :(得分:0)

您可以通过处理AppDomain.CurrentDomain.AssemblyResolve事件轻松完成所需的操作。

然后在附加的事件处理程序中调用:

Assembly assembly = Assembly.LoadFile(path: assemblyPath); return assembly; // return resolved assembly

assemblyPath是程序试图获取的DLL的绝对路径,您可以通过在运行时扫描DLL文件夹然后获取包含缺少的DLL名称的路径来获取该文件。