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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:55:10  来源:igfitidea点击:

How do I get the directory from a file's full path?

c#.netfilefile-iodirectory

提问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 Pathand FileInfoare both found in the namespace System.IO.

请注意,PathFileInfo都在命名空间中找到System.IO

回答by Grzenio

Path.GetDirectoryName(filename);

回答by Brandon

You can use Path.GetDirectoryNameand just pass in the filename.

您可以使用Path.GetDirectoryName并传入文件名。

MSDN Link

MSDN链接

回答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 FileInfomay have advantages.

如果您正在使用路径做其他事情,则FileInfo可能有优势。

回答by Derek W

If you are working with a FileInfoobject, then there is an easy way to extract a stringrepresentation of the directory's full path via the DirectoryNameproperty.

如果您正在处理一个FileInfo对象,那么有一种简单的方法可以string通过DirectoryName属性提取目录完整路径的表示形式。

Description of the FileInfo.DirectoryNameProperty 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.GetFullPathfor 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 directorylike "C:\Temp\"

GetPath("Filename.txt")返回current working directory喜欢"C:\Temp\"