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

发布于 2024-07-15 08:04:31 字数 153 浏览 5 评论 0原文

获取文件所在目录的最简单方法是什么? 我用它来设置工作目录。

string filename = @"C:\MyDirectory\MyFile.bat";

在这个例子中,我应该得到“C:\MyDirectory”。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

静待花开 2024-07-22 08:04:31

如果您确实有绝对路径,请使用 Path .GetDirectoryName(路径)

如果您可能只有相对名称,请使用new FileInfo(path).Directory.FullName

请注意,PathFileInfo 均位于命名空间 System.IO 中。

If you definitely have an absolute path, use Path.GetDirectoryName(path).

If you might only have a relative name, use new FileInfo(path).Directory.FullName.

Note that Path and FileInfo are both found in the namespace System.IO.

你的往事 2024-07-22 08:04:31
System.IO.Path.GetDirectoryName(filename)
System.IO.Path.GetDirectoryName(filename)
如果没有 2024-07-22 08:04:31
Path.GetDirectoryName(filename);
Path.GetDirectoryName(filename);
月野兔 2024-07-22 08:04:31

您可以使用System.IO.Path.GetDirectoryName(fileName),或使用FileInfo.Directory将路径转换为FileInfo

如果您使用路径执行其他操作,FileInfo 类可能具有优势。

You can use System.IO.Path.GetDirectoryName(fileName), or turn the path into a FileInfo using FileInfo.Directory.

If you're doing other things with the path, the FileInfo class may have advantages.

白色秋天 2024-07-22 08:04:31

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

MSDN 链接

You can use Path.GetDirectoryName and just pass in the filename.

MSDN Link

情话已封尘 2024-07-22 08:04:31

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

通过 MSDN 对 FileInfo.DirectoryName 属性的描述:

获取表示目录完整路径的字符串。

示例用法:

string filename = @"C:\MyDirectory\MyFile.bat";
FileInfo fileInfo = new FileInfo(filename);
string directoryFullPath = fileInfo.DirectoryName; // contains "C:\MyDirectory"

链接到 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.

Description of the FileInfo.DirectoryName Property via 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.

两仪 2024-07-22 08:04:31

您可以使用以下方法获取当前的应用程序路径:

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

You can get the current Application Path using:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
二手情话 2024-07-22 08:04:31

首先,您必须使用 System.IO 命名空间。 然后;

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

或者

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

First, you have to use System.IO namespace. Then;

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

or

string newPath = Path.GetFullPath(openFileDialog1.FileName));
靖瑶 2024-07-22 08:04:31

大多数情况下,您可以使用 Path.GetFullPath 。
但是,如果您还想在文件名相对定位的情况下获取路径,则可以使用以下通用方法:

string GetPath(string filePath)
{
  return Path.GetDirectoryName(Path.GetFullPath(filePath))
}

例如:

GetPath("C:\Temp\Filename.txt") return "C:\Temp\"

GetPath("Filename.txt") 返回 当前工作目录 就像 "C:\温度\"

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:

string GetPath(string filePath)
{
  return Path.GetDirectoryName(Path.GetFullPath(filePath))
}

For example:

GetPath("C:\Temp\Filename.txt") return "C:\Temp\"

GetPath("Filename.txt") return current working directory like "C:\Temp\"

不知在何时 2024-07-22 08:04:31

就我而言,我需要找到(目录的)完整路径的目录名称,所以我简单地执行了以下操作:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文