我如何从一个文件的完整路径的目录? [英] 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";
在这个例子中,我应该得到的。C:\ MyDirectory
In this example, I should get "C:\MyDirectory".
推荐答案
如果你已经肯定了绝对路径,使用<一个href="http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx"><$c$c>Path.GetDirectoryName(path)$c$c>.
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
如果你可能只得到一个相对的名称,使用新的FileInfo(路径).Directory.FullName
。
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
注意路径
和的FileInfo
在命名空间都发现 System.IO
。
Note that Path
and FileInfo
are both found in the namespace System.IO
.
这篇关于我如何从一个文件的完整路径的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文