C# 如何在 .NET 控制台应用程序中获取应用程序的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/837488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the application's path in a .NET console application?
提问by JSmyth
How do I find the application's path in a console application?
如何在控制台应用程序中找到应用程序的路径?
In Windows Forms, I can use Application.StartupPath
to find the current path, but this doesn't seem to be available in a console application.
在Windows Forms 中,我可以Application.StartupPath
用来查找当前路径,但这在控制台应用程序中似乎不可用。
采纳答案by Sam Axe
System.Reflection.Assembly.GetExecutingAssembly()
.Location
1
System.Reflection.Assembly.GetExecutingAssembly()
. 1Location
Combine that with System.IO.Path.GetDirectoryName
if all you want is the directory.
System.IO.Path.GetDirectoryName
如果您想要的只是目录,请将其与它结合起来。
1As per Mr.Mindor's comment:
System.Reflection.Assembly.GetExecutingAssembly().Location
returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory.System.Reflection.Assembly.GetExecutingAssembly().CodeBase
will return the 'permanent' path of the assembly.
1根据 Mr.Mindor 的评论:
System.Reflection.Assembly.GetExecutingAssembly().Location
返回正在执行的程序集当前所在的位置,这可能是也可能不是该程序集在不执行时所在的位置。在卷影复制程序集的情况下,您将在临时目录中获得一个路径。System.Reflection.Assembly.GetExecutingAssembly().CodeBase
将返回程序集的“永久”路径。
回答by ist_lion
You may be looking to do this:
您可能希望这样做:
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
回答by ist_lion
You can use the following code to get the current application directory.
您可以使用以下代码获取当前应用程序目录。
AppDomain.CurrentDomain.BaseDirectory
回答by Steve Mc
Probably a bit late but this is worth a mention:
可能有点晚了,但值得一提的是:
Environment.GetCommandLineArgs()[0];
Or more correctly to get just the directory path:
或者更正确地获取目录路径:
System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
Edit:
编辑:
Quite a few people have pointed out that GetCommandLineArgs
is not guaranteed to return the program name. See The first word on the command line is the program name only by convention. The article does state that "Although extremely few Windows programs use this quirk (I am not aware of any myself)". So it is possible to 'spoof' GetCommandLineArgs
, but we are talking about a console application. Console apps are usually quick and dirty. So this fits in with my KISS philosophy.
不少人指出,GetCommandLineArgs
不能保证返回程序名称。请参阅命令行上的第一个单词只是约定俗成的程序名称。文章确实指出“尽管极少数 Windows 程序使用此怪癖(我自己不知道)”。所以有可能“欺骗” GetCommandLineArgs
,但我们正在谈论一个控制台应用程序。控制台应用程序通常又快又脏。所以这符合我的 KISS 哲学。
回答by rocketsarefast
For anyone interested in asp.net web apps. Here are my results of 3 different methods
对于任何对 asp.net 网络应用程序感兴趣的人。这是我使用 3 种不同方法的结果
protected void Application_Start(object sender, EventArgs e)
{
string p1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string p2 = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
string p3 = this.Server.MapPath("");
Console.WriteLine("p1 = " + p1);
Console.WriteLine("p2 = " + p2);
Console.WriteLine("p3 = " + p3);
}
result
结果
p1 = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a897dd66\ec73ff95\assembly\dl3\ff65202ddaade3_5e84cc01
p2 = C:\inetpub\SBSPortal_staging\
p3 = C:\inetpub\SBSPortal_staging
the app is physically running from "C:\inetpub\SBSPortal_staging", so the first solution is definitely not appropriate for web apps.
该应用程序从“C:\inetpub\SBSPortal_staging”实际运行,因此第一个解决方案绝对不适合网络应用程序。
回答by Mr.Mindor
You have two options for finding the directory of the application, which you choose will depend on your purpose.
您有两个选项可用于查找应用程序的目录,具体选择取决于您的目的。
// to get the location the assembly is executing from
//(not necessarily where the it normally resides on disk)
// in the case of the using shadow copies, for instance in NUnit tests,
// this will be in a temp directory.
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
//To get the location the assembly normally resides on disk or the install directory
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
//once you have the path you get the directory with:
var directory = System.IO.Path.GetDirectoryName(path);
回答by Herman
Assembly.GetEntryAssembly().Location
or Assembly.GetExecutingAssembly().Location
Assembly.GetEntryAssembly().Location
或者 Assembly.GetExecutingAssembly().Location
Use in combination with System.IO.Path.GetDirectoryName()
to get only the directory.
与 结合使用System.IO.Path.GetDirectoryName()
仅获取目录。
The paths from GetEntryAssembly()
and GetExecutingAssembly()
can be different, even though for most cases the directory will be the same.
来自GetEntryAssembly()
和的路径GetExecutingAssembly()
可以不同,即使在大多数情况下目录是相同的。
With GetEntryAssembly()
you have to be aware that this can return null
if the entry module is unmanaged (ie C++ or VB6 executable). In those cases it is possible to use GetModuleFileName
from the Win32 API:
随着GetEntryAssembly()
你要知道,这可以返回null
,如果输入模块未被管理(即C ++或VB6可执行文件)。在这些情况下,可以GetModuleFileName
从 Win32 API 使用:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
回答by fizzled
The answer above was 90% of what I needed, but returned a Uri instead of a regular path for me.
上面的答案是我需要的 90%,但返回了一个 Uri 而不是我的常规路径。
As explained in the MSDN forums post, How to convert URI path to normal filepath?, I used the following:
如 MSDN 论坛帖子中所述,如何将 URI 路径转换为普通文件路径?,我使用了以下内容:
// Get normal filepath of this assembly's permanent directory
var path = new Uri(
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
).LocalPath;
回答by Devarajan.T
You can create a folder name as Resources within the project using Solution Explorer,then you can paste a file within the Resources.
您可以使用解决方案资源管理器在项目中创建一个文件夹名称作为资源,然后您可以在资源中粘贴一个文件。
private void Form1_Load(object sender, EventArgs e) {
string appName = Environment.CurrentDirectory;
int l = appName.Length;
int h = appName.LastIndexOf("bin");
string ll = appName.Remove(h);
string g = ll + "Resources\sample.txt";
System.Diagnostics.Process.Start(g);
}
回答by ButtShock
you can use this one instead.
你可以用这个代替。
System.Environment.CurrentDirectory