我发现以下代码从自定义位置动态加载 dll:
private void Form1_Load(object sender, EventArgs e)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
}
private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyPath = Path.Combine(folderPath, "libs", new AssemblyName(args.Name).Name + ".dll");
if (File.Exists(assemblyPath) == false) return null;
Assembly assembly = Assembly.LoadFrom(assemblyPath);
return assembly;
}
private void button1_Click(object sender, EventArgs e)
{
var zip = ZipFile.Read("test.zip");
foreach (ZipEntry file in zip)
{
file.Extract(".", ExtractExistingFileAction.OverwriteSilently);
}
}
在某些情况下,此解决方案有效,但使用 ZipDotNet dll 我得到:
InnerException {"File or assembly name \"Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c\" or one of its dependencies, was not found. Operation is not supported. (Exception from HRESULT: 0x80131515)":"Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c"} System.Exception {System.IO.FileLoadException}
自从它通过以来,if (File.Exists(assemblyPath) == false) return null;
我猜它是加载 Ionic.Zip.dll 的依赖项的问题?我也将如何解决它们?