问题
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