C# 如何从文件的完整路径获取目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/674479/
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 do I get the directory from a file's full path?
提问by Even Mien
What is the simplest way to get the directory that a file is in? I'm using this to set a working directory.
获取文件所在目录的最简单方法是什么?我正在使用它来设置工作目录。
string filename = @"C:\MyDirectory\MyFile.bat";
In this example, I should get "C:\MyDirectory".
在这个例子中,我应该得到“C:\MyDirectory”。
采纳答案by Jon Skeet
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
如果您确实有绝对路径,请使用Path.GetDirectoryName(path)
.
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
如果您只能获得相对名称,请使用new FileInfo(path).Directory.FullName
.
Note that Path
and FileInfo
are both found in the namespace System.IO
.
请注意,Path
和FileInfo
都在命名空间中找到System.IO
。
回答by Grzenio
Path.GetDirectoryName(filename);
回答by Brandon
回答by Cherian
System.IO.Path.GetDirectoryName(filename)
回答by Reed Copsey
You can use System.IO.Path.GetDirectory(filename)
, or turn the path into a FileInfo
, and use FileInfo.Directory
.
您可以使用System.IO.Path.GetDirectory(filename)
,或将路径转换为FileInfo
,然后使用FileInfo.Directory
.
If you're doing other things with the path, the FileInfo
may have advantages.
如果您正在使用路径做其他事情,则FileInfo
可能有优势。
回答by Derek W
If you are working with a FileInfo
object, then there is an easy way to extract a string
representation of the directory's full path via the DirectoryName
property.
如果您正在处理一个FileInfo
对象,那么有一种简单的方法可以string
通过DirectoryName
属性提取目录完整路径的表示形式。
Description of the FileInfo.DirectoryName
Property via MSDN:
FileInfo.DirectoryName
通过 MSDN的属性描述:
Gets a string representing the directory's full path.
获取表示目录完整路径的字符串。
Sample usage:
示例用法:
string filename = @"C:\MyDirectory\MyFile.bat";
FileInfo fileInfo = new FileInfo(filename);
string directoryFullPath = fileInfo.DirectoryName; // contains "C:\MyDirectory"
Link to the MSDN documentation.
链接到MSDN 文档。
回答by Umut D.
First, you have to use System.IO namespace. Then;
首先,您必须使用 System.IO 命名空间。然后;
string filename = @"C:\MyDirectory\MyFile.bat";
string newPath = Path.GetFullPath(fileName);
or
或者
string newPath = Path.GetFullPath(openFileDialog1.FileName));
回答by thejustv
Use below mentioned code to get the folder path
使用下面提到的代码来获取文件夹路径
Path.GetDirectoryName(filename);
This will return "C:\MyDirectory"in your case
在您的情况下,这将返回“C:\MyDirectory”
回答by David Castro
You can get the current Application Path using:
您可以使用以下方法获取当前应用程序路径:
string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Good Luck!
祝你好运!
回答by Minh Nguyen
You can use Path.GetFullPath
for most of the case.
But if you want to get the path also in the case of the file name is relatively located then you can use the below generic method:
您可以Path.GetFullPath
在大多数情况下使用。但是,如果您还想在文件名相对定位的情况下获取路径,那么您可以使用以下通用方法:
string GetPath(string filePath)
{
return Path.GetDirectoryName(Path.GetFullPath(filePath))
}
For example:
例如:
GetPath("C:\Temp\Filename.txt")
return "C:\Temp\"
GetPath("C:\Temp\Filename.txt")
返回 "C:\Temp\"
GetPath("Filename.txt")
return current working directory
like "C:\Temp\"
GetPath("Filename.txt")
返回current working directory
喜欢"C:\Temp\"