简体   繁体   English

如何从文件的完整路径获取目录?

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

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”。

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 .请注意, PathFileInfo都位于命名空间System.IO中。

System.IO.Path.GetDirectoryName(filename)
Path.GetDirectoryName(filename);

You can use System.IO.Path.GetDirectoryName(fileName) , or turn the path into a FileInfo using FileInfo.Directory .您可以使用System.IO.Path.GetDirectoryName(fileName) ,或使用FileInfo.Directory FileInfo

If you're doing other things with the path, the FileInfo class may have advantages.如果您正在对路径执行其他操作,则FileInfo class 可能会有优势。

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”。

You can use Path.GetDirectoryName and just pass in the filename.您可以使用Path.GetDirectoryName并只传入文件名。

MSDN Link MSDN 链接

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 object,那么有一种简单的方法可以通过DirectoryName属性提取目录完整路径的string表示形式。

Description of the FileInfo.DirectoryName Property via MSDN:通过 MSDN 的FileInfo.DirectoryName属性的描述:

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 文档

You can get the current Application Path using:您可以使用以下方法获取当前的应用程序路径:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();

First, you have to use System.IO namespace.首先,您必须使用 System.IO 命名空间。 Then;然后;

string filename = @"C:\MyDirectory\MyFile.bat";
string newPath = Path.GetFullPath(fileName);

or要么

string newPath = Path.GetFullPath(openFileDialog1.FileName));

You can use Path.GetFullPath for most of the case.在大多数情况下,您可以使用Path.GetFullPath 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:但是如果你想在文件名相对定位的情况下也得到路径,那么你可以使用下面的通用方法:

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\"

In my case, I needed to find the directory name of a full path (of a directory) so I simply did:就我而言,我需要找到(目录的)完整路径的目录名称,所以我只是这样做了:

var dirName = path.Split('\\').Last();

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”。

 string path= @"C:\Users\username\Desktop\FolderName"
  string Dirctory = new FileInfo(path).Name.ToString();
//output FolderName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM