我的DLL是由第三方应用程序加载的,我们无法对其进行自定义。我的程序集必须位于它们自己的文件夹中。我不能将它们放入GAC (我的应用程序需要使用XCOPY进行部署)。当根DLL尝试从另一个DLL (在同一文件夹中)加载资源或类型时,加载失败(FileNotFound)。是否可以通过编程方式(从根DLL)将DLL所在的文件夹添加到程序集搜索路径?不允许我更改应用程序的配置文件。
发布于 2019-09-27 05:22:25
我从another (marked duplicate) question来到这里,是为了向App.Config文件添加探测标记。
我想对此添加一个附注- Visual studio已经生成了一个App.config文件,但是将探测标记添加到预先生成的运行时标记并不起作用!您需要一个单独的运行时标记,其中包含探测标记。简而言之,您的App.Config应该如下所示:
<?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)文件夹变得整洁,这正是我认为人们想要实现的目标。
:: 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%"https://stackoverflow.com/questions/1373100
复制相似问题