首页 > 解决方案 > 使用 AppDomain 加载程序集时出现 FileNotFoundException

问题描述

我想加载卸载在当前应用程序域之外找到的程序集。阅读以下主题后,

  1. AppDomain.Load() 失败并出现 FileNotFoundException
  2. 卸载装有 Assembly.LoadFrom() 的程序集<-我无法理解该线程的答案
  3. 在不同的 AppDomain 中加载/卸载程序集<- 我将目标程序集文件夹位置放在私有 bin 路径中,导致FileNotFoundException

我有以下代码引发FileNotFoundException.

// let's assume that the executable running this code is found at "F:\\Test.exe"

// this is the assembly I want to load
string pathToAssembly = "C:\\Documents\\hello.dll";

// copied directly from the 1st link
AppDomain dom = AppDomain.CreateDomain("some");     
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = pathToAssembly;
Assembly assembly = dom.Load(assemblyName); // raises FileNotFoundException

我检查了文档: https ://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-load-assemblies-into-an-application-domain https://docs.microsoft。 com/en-us/dotnet/framework/app-domains/how-to-unload-an-application-domain

但它没有使用 AppDomain从绝对路径加载和卸载。

基于第二个链接,AppDomains 在其当前可执行文件的目录路径中搜索程序集。当然,我需要解决这个问题,因为我需要在当前目录之外加载一个托管库。

而且,有关其他信息,我正在使用 .NET 框架 4.7.2 使用 WPF

TLDR:我需要更改 AppDomain 的“外观”目录吗?或者我能做些什么来解决这个问题?

标签: c#wpf

解决方案


推荐阅读