C# Set probing privatePath without app.config?

瘦欲@ 提交于 2019-11-30 11:58:56

You can do it for new AppDomains you create, I don't believe there is a way to do it in managed code for current/default AppDomain.

Edit: Creating AppDomain with private path: use AppDomain.CreateDomain and AppDomainSetup.PrivateBinPath

Darrell

You can also handle the AppDomain AssemblyResolve event like so:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

and:

 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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!