Relative Path to load a library?

拟墨画扇 提交于 2019-12-24 03:34:32

问题


I got the following compilation folders(simplified) :

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

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");

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


回答1:


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


来源:https://stackoverflow.com/questions/9016444/relative-path-to-load-a-library

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