探测在调试文件夹中工作,但没有其他地方

本文关键字:其他 调试 文件夹 工作 探测 | 更新日期: 2023-09-27 18:30:56

这是我的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="lib" />
      </assemblyBinding>
    </runtime>
</configuration>

我引用的 DLL 设置为不复制到本地,并且我已经将 DLL 复制到调试文件夹中的 lib 目录并且效果很好,但是当我将我的程序与我的库文件夹一起移动时,它不起作用。

当程序和库目录到不同的文件夹时,有没有办法让它工作?

探测在调试文件夹中工作,但没有其他地方

这个有点晚了,但有一个替代方案,把它添加到你的 Main 方法

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;

创建方法

    private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args)
    {
        AssemblyName assyName = new AssemblyName(args.Name);
        string filename = args.Name.ToLower().Split(',')[0];
        string path = "c:'some path or other'"
        string assembly = System.IO.Path.Combine(path, filename);
        if (!assembly.EndsWith(".dll"))
            assembly += ".dll";
        try
        {
            if (System.IO.File.Exists(assembly))
                return (Assembly.LoadFrom(assembly));
        }
        catch (Exception error)
        {
            // do whatever
        }
        return (null);
    }