简体   繁体   English

设置另一个库引用的dll的路径

[英]Set path of dlls referenced by another library

I have a C# project, a class library, wich is a module for another project, and it's output path is in a subfolder within the main-projects output path (ie outputpath = C:\\WHATEVER\\MainProject\\Modules. 我有一个C#项目,一个类库,它是另一个项目的模块,它的输出路径在主项目输出路径的子文件夹中(即outputpath = C:\\ WHATEVER \\ MainProject \\ Modules)。

In this Project, i reference a library (let's call it a.dll). 在这个项目中,我引用了一个库(我们称它为a.dll)。 this in itself works fine, but this a.dll needs other libraries at runtime to work (let's call them b.dll and c.dll). 这本身可以正常工作,但是此a.dll在运行时需要其他库才能起作用(我们将其称为b.dll和c.dll)。 I added these two files via Add -> Existing Item, and set "Copy to Output Directory" to "Copy always" for b.dll and c.dll. 我通过添加->现有项目添加了这两个文件,并将b.dll和c.dll的“复制到输出目录”设置为“总是复制”。

When i build the project, all three dlls are copied over into the output-path of the project (as expected), but a.dll can't seem to find them. 当我构建项目时,所有三个dll都被复制到项目的输出路径中(如预期的那样),但是a.dll似乎找不到它们。 As soon as i copy b.dll and c.dll into the main-projects folder (where the .exe is) a.dll can suddenly find them. 一旦我将b.dll和c.dll复制到main-projects文件夹(.exe所在的文件夹)中,a.dll就会突然找到它们。

I don't want to have b.dll and c.dll in the main-projects folder, because they are only part of the module i'm currently developing, and not the main-project itself. 我不想在主项目文件夹中包含b.dll和c.dll,因为它们只是我当前正在开发的模块的一部分,而不是主项目本身。 How (in Visual Studio 2015) do i tell a.dll where to look for b.dll and c.dll? 我如何(在Visual Studio 2015中)告诉a.dll在哪里寻找b.dll和c.dll? I tried adding the probing-part in the appconfig (as suggested here: Reference a DLL from another DLL ), but that had no effect. 我尝试在appconfig中添加探测部分(如此处建议: 从另一个DLL引用一个DLL ),但这没有任何效果。 I do not have the sourcecode for a.dll. 我没有a.dll的源代码。

The first part of this answer is based on what can be found here: https://stackoverflow.com/a/1892515/937093 该答案的第一部分基于此处的内容: https : //stackoverflow.com/a/1892515/937093

The method being used is basically the same as the one referenced in what you linked, but instead of using the XAML method, it uses the C#/code method (easier to debug/figure out what is going on). 所使用的方法与链接中所引用的方法基本相同,但不是使用XAML方法,而是使用C#/ code方法(更容易调试/弄清正在发生的事情)。

Add your custom path as described in that answer. 按照该答案中的说明添加自定义路径。 Do that in the Main method of your application. 在应用程序的Main方法中执行此操作。 Start by adding the relative path to that folder, if that doesn't work, take a look here: explanation of fuslogvw , to figure out where your program is looking, maybe a path is wrong or you don't have permission to look into that specific directory. 首先向该文件夹添加相对路径,如果不起作用,请看这里: fuslogvw的说明 ,找出程序在哪里寻找,可能是路径错误或您无权查看该特定目录。

If all fails and/or you are getting nowhere here, you can also try the following: 如果所有方法都失败了,并且/或者您在这里一无所获,还可以尝试以下操作:

In C#/.NET you've an event AppDomain.CurrentDomain.AssemblyResolve , you can read about it here . 在C#/。NET中,您有一个事件AppDomain.CurrentDomain.AssemblyResolve ,您可以在这里阅读。

In short, that even fires if .NET can't find an assembly and the eventhandler of that event allows you to resolve an assembly for .NET. 简而言之,即使.NET找不到程序集,该事件也将触发,并且该事件的事件处理程序允许您为.NET解析程序集。

Inside the event you could do something like this: AssemblyName reqAssemblyName = new AssemblyName(args.Name); 在事件内部,您可以执行以下操作:AssemblyName reqAssemblyName = new AssemblyName(args.Name);

if (reqAssemblyName.Name.ToLowerInvariant() == "b")
{
    return Assembly.LoadFrom(pathToAssemblyB);
}
else if (reqAssemblyName.Name.ToLowerInvariant() == "c")
{
    return Assembly.LoadFrom(pathToAssemblyC);
}
else
{
   return null;
}

To get the path to the two assemblies you can use Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mysubfolder","b.dll"); 要获取两个程序集的路径,可以使用Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mysubfolder","b.dll");

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

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