我正在运行时加载一个dll,方法如下:
var DLL = Assembly.LoadFile(@"..\..\BuildDLLs\myDLL.dll");
我遇到了一个ArgumentException,要求传入一个绝对路径。
但是我不想使用绝对路径,我想使用相对路径。
我该怎么做?
我正在运行时加载一个dll,方法如下:
var DLL = Assembly.LoadFile(@"..\..\BuildDLLs\myDLL.dll");
我遇到了一个ArgumentException,要求传入一个绝对路径。
但是我不想使用绝对路径,我想使用相对路径。
我该怎么做?
简单,多做一步:
var dllFile = new FileInfo(@"..\..\BuildDLLs\myDLL.dll");
var DLL = Assembly.LoadFile(dllFile.FullName);
我不知道是否有办法使用相对路径,可能其他人有答案。但是您可以通过相对路径构建一个绝对路径并使用它。
// Gets the folder path in which your .exe is located
var parentFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
// Makes the absolute path
var absolutePath = Path.Combine(parentFolder, "\BuildDLLs\myDLL.dll");
// Load the DLL using the absolute path
var DLL = Assembly.LoadFile(absolutePath);
System.Reflection.Assembly.GetEntryAssembly().Location;
//to get the path of your main applications .exe
并且
Directory.GetParent(String)
//to move your way upwards in your folder sturcture
那么
Path.Combine(String, String)
/*to combine the path you just navigated to inside your project with the knowledge of where you can find your .dll inside of your folder sturcture and combine them into one path again.*/