C# 获取当前的 dll 所在的路径
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
- 通过
CodeBase
得到一个 URI 格式的路径; - 用
UriBuild.UnescapeDataString
去掉前缀 File://; - 用
GetDirectoryName
把它变成正常的 windows 格式。
转自:https://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in