我有一条路径,我需要确定它是目录还是文件。
这是确定路径是否为文件的最佳方法吗?
string file = @"C:\Test\foo.txt";
bool isFile = !System.IO.Directory.Exists(file) &&
System.IO.File.Exists(file);
对于目录,我会颠倒逻辑。
string directory = @"C:\Test";
bool isDirectory = System.IO.Directory.Exists(directory) &&
!System.IO.File.Exists(directory);
如果两者都不存在那么我就不会去任何一个分支。所以假设它们都存在。
答案 0 :(得分:109)
使用:
System.IO.File.GetAttributes(string path)
并检查返回的FileAttributes
结果是否包含值FileAttributes.Directory
:
bool isDir = (File.GetAttributes(path) & FileAttributes.Directory)
== FileAttributes.Directory;
答案 1 :(得分:53)
我认为这是您只需要两次检查的最简单方法:
string file = @"C:\tmp";
if (System.IO.Directory.Exists(file))
{
// do stuff when file is an existing directory
}
else if (System.IO.File.Exists(file))
{
// do stuff when file is an existing file
}
答案 2 :(得分:10)
您可以使用一些互操作代码执行此操作:
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern bool PathIsDirectory([MarshalAsAttribute(UnmanagedType.LPWStr), In] string pszPath);
进一步澄清一些评论......
在.NET中引入非托管代码并不比.NET中任何其他文件或I / O相关调用更具危险性,因为它们最终都会调用非托管代码。
这是使用字符串的单个函数调用。您不是通过调用此函数来引入任何新数据类型和/或内存使用情况。是的,您确实需要依赖非托管代码来正确清理,但您最终会依赖大多数与I / O相关的调用。
供参考,以下是Reflector的File.GetAttributes(字符串路径)的代码:
public static FileAttributes GetAttributes(string path)
{
string fullPathInternal = Path.GetFullPathInternal(path);
new FileIOPermission(FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand();
Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
int errorCode = FillAttributeInfo(fullPathInternal, ref data, false, true);
if (errorCode != 0)
{
__Error.WinIOError(errorCode, fullPathInternal);
}
return (FileAttributes) data.fileAttributes;
}
正如您所看到的,它还调用非托管代码以检索文件属性,因此关于引入非托管代码的争论是无效的。同样,关于完全保留在托管代码中的论点。没有托管代码实现来执行此操作。甚至调用File.GetAttributes()作为其他答案建议也有调用unmanged代码的相同“问题”,我相信这是确定路径是否是目录的更可靠的方法。
编辑回答@Christian K关于CAS的评论。我相信GetAttributes提出安全需求的唯一原因是它需要读取文件的属性,以便确保调用代码有权这样做。这与底层操作系统检查(如果有)不同。您可以随时围绕对PathIsDirectory的P / Invoke调用创建一个包装函数,如果需要,还需要某些CAS权限。
答案 3 :(得分:7)
假设目录存在......
bool isDir = (File.GetAttributes(path) & FileAttributes.Directory)
== FileAttributes.Directory;
答案 4 :(得分:3)
检查出来:
/// <summary>
/// Returns true if the given file path is a folder.
/// </summary>
/// <param name="Path">File path</param>
/// <returns>True if a folder</returns>
public bool IsFolder(string path)
{
return ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory);
}
答案 5 :(得分:2)
阅读文件属性:
FileAttributes att = System.IO.File.GetAttributes(PATH_TO_FILE);
检查目录标志。
答案 6 :(得分:1)
鉴于特定的路径字符串不能同时代表目录和文件,那么以下工作正常并为其他操作打开了大门。
bool isFile = new FileInfo(path).Exists;
bool isDir = new DirectoryInfo(path).Exists;
如果您正在使用文件系统,则使用FileInfo
和DirectoryInfo
要比使用字符串简单得多。
答案 7 :(得分:-3)
嗯,看起来Files
类(在java.nio
中)实际上有一个静态isDirectory
方法。所以,我认为你实际上可以使用以下内容:
Path what = ...
boolean isDir = Files.isDirectory(what);