C# 获取当前文件夹路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15653921/
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
Get current folder path
提问by user2214609
I want to create a program that converts files. I would like the user to be able to place the executable file in any directory, and when executing that program (double-clicking on the .exe) I want the program to process all the files within the current folder where the exe file exists. How can the program determine the path in which it is currently executing?
我想创建一个转换文件的程序。我希望用户能够将可执行文件放在任何目录中,并且在执行该程序(双击 .exe)时,我希望该程序处理exe 文件所在的当前文件夹中的所有文件。程序如何确定它当前正在执行的路径?
I tried System.Windows.Forms.Application.StartupPath
but that seems to be the wrong way.
我试过了,System.Windows.Forms.Application.StartupPath
但这似乎是错误的方式。
Any ideas?
有任何想法吗?
采纳答案by Thorsten Dittmar
You should notuse Directory.GetCurrentDirectory()
in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.
你应该不使用Directory.GetCurrentDirectory()
你的情况,当前目录可以从执行文件夹不同,尤其是当你通过一个快捷方式执行程序。
It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
for your purpose. This returns the pathname where the currently executing assembly resides.
最好Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
用于您的目的。这将返回当前正在执行的程序集所在的路径名。
While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner G?nülsaid in his answer,
虽然我建议的方法允许您区分执行程序集、入口程序集或任何其他加载的程序集,正如Soner G?nül在他的回答中所说,
System.IO.Path.GetDirectoryName(Application.ExecutablePath);
may also be sufficient. This would be equal to
也可能足够了。这将等于
System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
回答by fhnaseer
Use this,
用这个,
var currentDirectory = System.IO.Directory.GetCurrentDirectory();
You can use this as well.
你也可以使用这个。
var currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
回答by Hossein Narimani Rad
回答by Soner G?nül
string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Returns the directory information for the specified path string.
返回指定路径字符串的目录信息。
From Application.ExecutablePath
Gets the path for the executable file that started the application, including the executable name.
获取启动应用程序的可执行文件的路径,包括可执行文件的名称。
回答by Tomtom
1.
1.
Directory.GetCurrentDirectory();
2.
2.
Thread.GetDomain().BaseDirectory
3.
3.
Environment.CurrentDirectory
回答by hakan
System.AppDomain.CurrentDomain.BaseDirectory
This will give you running directory of your application. This even works for web applications. Afterwards you can reach your file.
这将为您提供应用程序的运行目录。这甚至适用于 Web 应用程序。之后,您可以访问您的文件。
回答by kmote
I created a simple console application with the following code:
我使用以下代码创建了一个简单的控制台应用程序:
Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(System.Environment.CurrentDirectory);
Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
Console.WriteLine(Environment.CurrentDirectory);
I copied the resulting executable to C:\temp2
. I then placed a shortcut to that executable in C:\temp3
, and ran it (once from the exe itself, and once from the shortcut). It gave the following outputs both times:
我将生成的可执行文件复制到C:\temp2
. 然后我在 中放置了该可执行文件的快捷方式C:\temp3
,并运行它(一次从 exe 本身,一次从快捷方式)。它两次都给出了以下输出:
C:\temp2
C:\temp2\
C:\temp2
C:\temp2
C:\temp2
While I'm sure there must be somecockamamie reason to explain why there are five different methodsthat do virtually the exact same thing, I certainly don't know what it is. Nevertheless, it would appear that under most circumstances, you are free to choose whichever one you fancy.
虽然我确信一定有一些奇怪的理由来解释为什么有五种不同的方法可以做几乎完全相同的事情,但我当然不知道它是什么。尽管如此,在大多数情况下,您似乎可以自由选择您喜欢的任何一种。
UPDATE:I modified the Shortcut properties, changing the "Start In:" field to C:\temp3
. This resulted in the following output:
更新:我修改了快捷方式属性,将“开始于:”字段更改为C:\temp3
. 这导致了以下输出:
C:\temp2
C:\temp2\
C:\temp3
C:\temp3
C:\temp3
...which demonstrates at least someof the distinctions between the different methods.
...这至少证明了不同方法之间的一些区别。
回答by farosch
Use Application.StartupPath
for the best result imo.
使用Application.StartupPath
海事组织最好的结果。
回答by yonatan
for .NET COREuse System.AppContext.BaseDirectory
对于.NET CORE使用System.AppContext.BaseDirectory
(as a replacement for AppDomain.CurrentDomain.BaseDirectory
)
(作为替代AppDomain.CurrentDomain.BaseDirectory
)
回答by Howard Rothenburg
If you want the exe path you can use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
如果你想要你可以使用的exe路径 System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);