获取当前程序集的路径

时间:2023-02-05 17:33:11

How do I get the path of the current assembly? I need to get data from some paths relative to the location of hte current assembly (.dll).

如何获得当前组件的路径?我需要从某些路径获取相对于当前程序集(.dll)位置的数据。

I thought someone told me to use the reflection namespace but I can't find anything in there.

我以为有人告诉我使用反射命名空间,但我找不到任何内容。

4 个解决方案

#1


73  

You can use:

您可以使用:

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

Some suggestions in the comments are to pass that through System.Uri.UnescapeDataString (from vvnurmi) to ensure that any percent-encoding is handled, and to use Path.GetFullpath (from TrueWill) to ensure that the path is in standard Windows form (rather than having slashes instead of backslashes). Here's an example of what you get at each stage:

注释中的一些建议是通过System.Uri.UnescapeDataString(来自vvnurmi)传递它以确保处理任何百分比编码,并使用Path.GetFullpath(来自TrueWill)来确保路径采用标准Windows格式(而不是斜杠而不是反斜杠)。以下是您在每个阶段获得的内容的示例:

string s = Assembly.GetExecutingAssembly().CodeBase;
Console.WriteLine("CodeBase: [" + s + "]");
s = (new Uri(s)).AbsolutePath;
Console.WriteLine("AbsolutePath: [" + s + "]");
s = Uri.UnescapeDataString(s);
Console.WriteLine("Unescaped: [" + s + "]");
s = Path.GetFullPath(s);
Console.WriteLine("FullPath: [" + s + "]");

Output if we're running C:\Temp\Temp App\bin\Debug\TempApp.EXE:

如果我们正在运行C:\ Temp \ Temp App \ bin \ Debug \ TempApp.EXE,则输出:

CodeBase: [file:///C:/Temp/Temp App/bin/Debug/TempApp.EXE]
AbsolutePath: [C:/Temp/Temp%20App/bin/Debug/TempApp.EXE]
Unescaped: [C:/Temp/Temp App/bin/Debug/TempApp.EXE]
FullPath: [C:\Temp\Temp App\bin\Debug\TempApp.EXE]

#2


44  

System.Reflection.Assembly.GetExecutingAssembly().Location

System.Reflection.Assembly.GetExecutingAssembly()位置。

#3


6  

Just to make it clear:

只是为了说清楚:

Assembly.GetExecutingAssembly().Location

#4


2  

I prefer

我更喜欢

new System.Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;

EscapedCodeBase covers the scenario where your local path might have an invalid URI char in it (see https://*.com/a/21056435/852208)

EscapedCodeBase涵盖了本地路径中可能包含无效URI char的场景(请参阅https://*.com/a/21056435/852208)

LocalPath includes the full path for both local paths and unc paths, where AbsolutePath trims off "\\server"

LocalPath包括本地路径和unc路径的完整路径,其中AbsolutePath修剪掉“\\ server”

#1


73  

You can use:

您可以使用:

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

Some suggestions in the comments are to pass that through System.Uri.UnescapeDataString (from vvnurmi) to ensure that any percent-encoding is handled, and to use Path.GetFullpath (from TrueWill) to ensure that the path is in standard Windows form (rather than having slashes instead of backslashes). Here's an example of what you get at each stage:

注释中的一些建议是通过System.Uri.UnescapeDataString(来自vvnurmi)传递它以确保处理任何百分比编码,并使用Path.GetFullpath(来自TrueWill)来确保路径采用标准Windows格式(而不是斜杠而不是反斜杠)。以下是您在每个阶段获得的内容的示例:

string s = Assembly.GetExecutingAssembly().CodeBase;
Console.WriteLine("CodeBase: [" + s + "]");
s = (new Uri(s)).AbsolutePath;
Console.WriteLine("AbsolutePath: [" + s + "]");
s = Uri.UnescapeDataString(s);
Console.WriteLine("Unescaped: [" + s + "]");
s = Path.GetFullPath(s);
Console.WriteLine("FullPath: [" + s + "]");

Output if we're running C:\Temp\Temp App\bin\Debug\TempApp.EXE:

如果我们正在运行C:\ Temp \ Temp App \ bin \ Debug \ TempApp.EXE,则输出:

CodeBase: [file:///C:/Temp/Temp App/bin/Debug/TempApp.EXE]
AbsolutePath: [C:/Temp/Temp%20App/bin/Debug/TempApp.EXE]
Unescaped: [C:/Temp/Temp App/bin/Debug/TempApp.EXE]
FullPath: [C:\Temp\Temp App\bin\Debug\TempApp.EXE]

#2


44  

System.Reflection.Assembly.GetExecutingAssembly().Location

System.Reflection.Assembly.GetExecutingAssembly()位置。

#3


6  

Just to make it clear:

只是为了说清楚:

Assembly.GetExecutingAssembly().Location

#4


2  

I prefer

我更喜欢

new System.Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;

EscapedCodeBase covers the scenario where your local path might have an invalid URI char in it (see https://*.com/a/21056435/852208)

EscapedCodeBase涵盖了本地路径中可能包含无效URI char的场景(请参阅https://*.com/a/21056435/852208)

LocalPath includes the full path for both local paths and unc paths, where AbsolutePath trims off "\\server"

LocalPath包括本地路径和unc路径的完整路径,其中AbsolutePath修剪掉“\\ server”