加载库的相对路径? [英] Relative Path to load a library?

查看:122
本文介绍了加载库的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下编译文件夹(简体):

I got the following compilation folders(simplified) :

D:\MySolution\Project1\bin\Debug
D:\MySolution\Project2\bin\Debug

我的Project1Project2上有一个引用,但是在代码中,我必须从Project1加载类,因此我使用了有效的代码:

My Project1 has a reference on Project2 but in the code I have to load a class from Project1 so I used this code that works :

var project2ref = Assembly.LoadFrom(@"..\..\..\Project1\bin\Debug\Project1.dll")
                                .CreateInstance("MyNamespace.MyClass");

这在VS中有效,但是当我使用软件的安装版本时会崩溃... 相对路径有哪些替代方法?

This works in VS but it crashes when I use an install version of my software... What are the alternatives for the relative path ?

推荐答案

构建解决方案时,将project1的dll放入project2的bin文件夹中. 因此,您可以通过检索project1.dll的汇编路径来获得project2.dll的引用.下面的代码将在project1内部执行,并将获取包含两个dll的项目的bin路径.

You will have the dll of project1 into the bin folder of project2 when you'll build the solution. So, you can get a reference of the project2.dll by retrieving the assembly path of project1.dll. The below code will be executed inside the project1 and will get the bin path of the project who will contains both the dll.

System.Reflection.Assembly.GetExecutingAssembly().Location

这篇关于加载库的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆