首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在.NET中如何在运行时将文件夹添加到程序集搜索路径?

在.NET中如何在运行时将文件夹添加到程序集搜索路径?
EN

Stack Overflow用户
提问于 2009-09-03 12:26:41
回答 8查看 105.2K关注 0票数 138

我的DLL是由第三方应用程序加载的,我们无法对其进行自定义。我的程序集必须位于它们自己的文件夹中。我不能将它们放入GAC (我的应用程序需要使用XCOPY进行部署)。当根DLL尝试从另一个DLL (在同一文件夹中)加载资源或类型时,加载失败(FileNotFound)。是否可以通过编程方式(从根DLL)将DLL所在的文件夹添加到程序集搜索路径?不允许我更改应用程序的配置文件。

EN

Stack Overflow用户

发布于 2019-09-27 05:22:25

我从another (marked duplicate) question来到这里,是为了向App.Config文件添加探测标记。

我想对此添加一个附注- Visual studio已经生成了一个App.config文件,但是将探测标记添加到预先生成的运行时标记并不起作用!您需要一个单独的运行时标记,其中包含探测标记。简而言之,您的App.Config应该如下所示:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

  <!-- Discover assemblies in /lib -->
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="lib" />
    </assemblyBinding>
  </runtime>
</configuration>

这花了一些时间才弄清楚,所以我把它贴在这里。也归功于The PrettyBin NuGet Package。它是一个自动移动dlls的包。我喜欢一种更手动的方法,所以我没有使用它。

还有-这里是一个构建后脚本,它将所有.dll/.xml/.pdb复制到/Lib。这会使/debug (或/release)文件夹变得整洁,这正是我认为人们想要实现的目标。

代码语言:javascript
运行
复制
:: Moves files to a subdirectory, to unclutter the application folder
:: Note that the new subdirectory should be probed so the dlls can be found.
SET path=$(TargetDir)\lib
if not exist "%path%" mkdir "%path%"
del /S /Q "%path%"
move /Y $(TargetDir)*.dll "%path%"
move /Y $(TargetDir)*.xml "%path%"
move /Y $(TargetDir)*.pdb "%path%"
票数 6
EN
查看全部 8 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1373100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档