C# 在没有 app.config 的情况下设置探测 privatePath?

标签 c# path app-config private probing

我有一个 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/

相关文章:

c# - 将设置添加到 app.config 或者将设置添加到 *.exe.config 就足够了吗?

c# - 在 C# Dll 中保存加密数据和私钥的安全方法

c# - 有没有办法像在套接字中一样从 TcpClient 获取远程 IP 端点?

ruby - 如何在 ubuntu 12.0.4 上将 usr/local/bin 添加到路径环境变量?

c# - LINQ - 在集合中过滤集合

algorithm - 来自给定节点的最长路径近似算法

java - 获取两个像素之间的所有像素

.net - Web.config 转换可以与 App.config 文件一起使用吗?

c# - 防止从 GAC 加载 dll

c# - 使用方法的 MethodHandle 作为键来缓存无参数方法的结果是否安全?