如何从文件的完整路径获取目录? [英] How do I get the directory from a file's full path?
本文介绍了如何从文件的完整路径获取目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string filename = @C:\MyDirectory\MyFile.bat ;
在这个例子中,我应该得到C:\MyDirectory。
解决方案
如果您绝对有绝对路径,请使用 Path.GetDirectoryName(path)
。
如果您只能获得相对名称,请使用新的FileInfo(路径).Directory.FullName
。
请注意, Path
和 FileInfo
都位于命名空间 System.IO中
。
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".
解决方案
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
Note that Path
and FileInfo
are both found in the namespace System.IO
.
这篇关于如何从文件的完整路径获取目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文