简体   繁体   English

从不同于exe目录的文件夹引用.net dll

[英]to refer .net dll from different folder than the exe directory

I have a structure:- \\bin\\debug\\abc.exe and \\Libs\\win32\\xyz.dll . 我有一个结构:- \\bin\\debug\\abc.exe\\Libs\\win32\\xyz.dll Now I need to refer xyz.dll so as to run my abc.exe . 现在,我需要引用xyz.dll来运行abc.exe I tried with "probing" tag in app.config but in that case the possibility was only when I had 'Libs ' folder in ' debug ' folder ie where .exe is present. 我尝试在app.config使用“探测”标签,但是在那种情况下,可能性只有当我在“ debug ”文件夹中有'Libs ”文件夹时,即存在.exe地方。 But I want to come 2 folders out from .exe and then go into \\Libs\\win32 to refer to .dll . 但是我想从.exe中取出2个文件夹,然后进入\\Libs\\win32以引用.dll。 Please suggest me what should I do. 请建议我该怎么办。

One option is handling AssemblyResolve event, every time .NET couldn't find required assembly in current path, it will trigger AssemblyResolve event: 一种选择是处理AssemblyResolve事件,每次.NET在当前路径中找不到所需的程序集时,它将触发AssemblyResolve事件:

{
    // Execute in startup
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
}

private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
    string RESOURCES = ".resources";
    try
    {
        /* Extract assembly name */
        string[] sections =  args.Name.Split(new char[] { ',' });
        if (sections.Length == 0) return null;

        string assemblyName = sections[0];

        /* If assembly name contains ".resource", you don't need to load it*/
        if (assemblyName.Length >= RESOURCES.Length &&
                assemblyName.LastIndexOf(RESOURCES) == assemblyName.Length - RESOURCES.Length)
        {
            return null;
        }

        /* Load assembly to current domain (also you can use simple way to load) */
        string assemblyFullPath = "..//..//Libs//" + assemblyName;
        FileStream io = new FileStream(assemblyNameWithExtension, FileMode.Open, FileAccess.Read);
        if (io == null) return null;
        BinaryReader binaryReader = new BinaryReader(io);
        Assembly assembly = Assembly.Load(binaryReader.ReadBytes((int)io.Length));

        return assembly;
   }
   catch(Exception ex)
   {}
}

*Another option is loading all of your required assemblies to current domain at your project start-up. *另一个选项是在项目启动时将所有必需的程序集加载到当前域。

You use ..\\ in the file path to move up a directory. 您在文件路径中使用.. \\向上移动目录。
So if you're in \\bin\\debug\\abc.exe then your reference to \\Libs\\win32\\xyz.dll would be 因此,如果您在\\ bin \\ debug \\ abc.exe中,则对\\ Libs \\ win32 \\ xyz.dll的引用将是

..\..\Libs\win32\xyz.dll

This should only be necessary when building your projects, when it's built if your executable is referencing the dll correctly it only needs to be put in the same folder as the dll. 仅当构建项目时才需要这样做,如果可执行文件正确引用了dll,则在构建项目时,只需将其与dll放在同一文件夹中即可。

Unless of course you're using dllimport or something where you need to know the exact path of the dll during runtime. 除非您当然使用的是dllimport或在运行时需要知道dll确切路径的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM