我有一个 C# 应用程序,为了组织它的文件,我在名为“Data”的文件夹中放置了一些 DLL。我希望 EXE 像检查当前目录一样检查此文件夹中的 DLL。如果我使用此信息创建 App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Data" />
</assemblyBinding>
</runtime>
</configuration>
它工作没有问题。我不想有一个 App.Config。有没有一种方法可以在不使用 app.config 的情况下设置探测路径?
最佳答案
您还可以像这样处理 AppDomain AssemblyResolve
事件:
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
和:
private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var probingPath = pathToYourDataFolderHere;
var assyName = new AssemblyName(args.Name);
var newPath = Path.Combine(probingPath, assyName.Name);
if (!newPath.EndsWith(".dll"))
{
newPath = newPath + ".dll";
}
if (File.Exists(newPath))
{
var assy = Assembly.LoadFile(newPath);
return assy;
}
return null;
}
关于C# 在没有 app.config 的情况下设置探测 privatePath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10345240/