C# “FileNotFoundException-无法从项目库加载文件或程序集”,即使使用了LoadFrom()
我有一个VS2017项目,由以下部分组成:C# “FileNotFoundException-无法从项目库加载文件或程序集”,即使使用了LoadFrom(),c#,.net,visual-studio,dll,dynamic-loading,C#,.net,Visual Studio,Dll,Dynamic Loading,我有一个VS2017项目,由以下部分组成: - Main console application `App.exe`, compiled in .NET Framework 4.6.1 --- (referenced by `App.exe`) Custom dynamic library `LibOne.dll`, compiled in .NET Standard 2.0 ----- (referenced by `LibOne.dll`) Another dynamic library `
- Main console application `App.exe`, compiled in .NET Framework 4.6.1
--- (referenced by `App.exe`) Custom dynamic library `LibOne.dll`, compiled in .NET Standard 2.0
----- (referenced by `LibOne.dll`) Another dynamic library `LibTwo.dll`, compiled in .NET Framework 3.5
默认情况下,编译时LibOne.dll和LibTwo.dll都复制到App.exe文件夹中。相反,我需要防止LibTwo.dll被复制,使程序从系统的另一个路径C:\path\u to\u LibTwo查找它
因此,我将LibTwo.dll的Copy Local and Specific Version设置为FALSE,在App和LibOne项目中,现在程序在调用LibOne.dll的方法时抛出此错误:
这是合法的,但问题仍然存在,即使:
C:\Path_To_LibTwo(其中包含LibTwo.dll)已添加到系统的路径中
我已将此行添加为我的应用程序的第一件事:
System.Reflection.Assembly。LoadFrom@C:\Path\u To\u libtwoo\libtwoo.dll;注意:此行正确返回LibTwo的程序集,因此可以找到它
更新:我已将此添加到App.config:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="LibTwo" culture="neutral" publicKeyToken="null" />
<codeBase href="file:///C:/Path_To_LibTwo/LibTwo.dll" />
</dependentAssembly>
</assemblyBinding>
LibTwo在LibOne或App中都是编译时引用吗?我不太确定你说的编译时引用是什么意思,反正它是这样添加的:1右键单击LibOne项目2添加。。。引用。。。3选择LibTwo.dll 4 Copy Local=false。我也尝试了同样的方法,但直接将其添加到App.exe项目中;否则,动态加载的意义何在?LibOne的代码调用LibTwo的方法。如果我将其从引用中删除,LibOne将不会编译。如果这是为了动态加载,那么我相信通过反射调用LibTwo的方法而不是引用它会更有意义。
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="LibTwo" culture="neutral" publicKeyToken="null" />
<codeBase href="file:///C:/Path_To_LibTwo/LibTwo.dll" />
</dependentAssembly>
</assemblyBinding>
Could not load file or assembly 'LibTwo, Version=12.4.0.61, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
The private assembly was located outside the appbase directory. (Exception from HRESULT: 0x80131041)