C# 如何确定对象是文件还是文件夹
我试图确定给定的路径是指向文件还是指向目录。目前我的逻辑非常简单,包括以下检查C# 如何确定对象是文件还是文件夹,c#,file,file-io,filepath,C#,File,File Io,Filepath,我试图确定给定的路径是指向文件还是指向目录。目前我的逻辑非常简单,包括以下检查 if (sourcePath.Contains(".")) // then treat this path as a file 上面的问题是文件夹名称中也可以有句点。我希望能够确定给定的路径是文件的路径,而不必尝试实例化filestream类型并尝试访问它或类似的东西 有没有办法做到这一点 提前感谢您可以使用以下方法: 如果路径描述目录,则 方法返回false 因此: 还有一种方法,文档中给出了以下示例: if(
if (sourcePath.Contains(".")) // then treat this path as a file
上面的问题是文件夹名称中也可以有句点。我希望能够确定给定的路径是文件的路径,而不必尝试实例化filestream类型并尝试访问它或类似的东西
有没有办法做到这一点
提前感谢您可以使用以下方法:
如果路径描述目录,则
方法返回false
因此:
还有一种方法,文档中给出了以下示例:
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
您可以使用以下方法:
如果路径描述目录,则
方法返回false
因此:
还有一种方法,文档中给出了以下示例:
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
System.IO.File.existsyourfilename在这里就可以了。如果路径不是文件的路径,则返回false。如果路径不存在,它也将返回false,因此请小心。public bool IsFolder(string path)
{
return ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory);
}
VB.Net:
Public Function IsFolder(path As String) As Boolean
Return ((File.GetAttributes(path) And FileAttributes.Directory) = FileAttributes.Directory)
End Function
如果文件不存在,此函数将引发文件未找到异常。所以你必须抓住它,或者使用达林·迪米特罗的方法
Try
Dim isExistingFolder As Boolean = IsFolder(path)
Dim isExistingFile = Not isExistingFolder
Catch fnfEx As FileNotFoundException
'.....
End Try
C:
VB.Net:
Public Function IsFolder(path As String) As Boolean
Return ((File.GetAttributes(path) And FileAttributes.Directory) = FileAttributes.Directory)
End Function
如果文件不存在,此函数将引发文件未找到异常。所以你必须抓住它,或者使用达林·迪米特罗的方法
Try
Dim isExistingFolder As Boolean = IsFolder(path)
Dim isExistingFile = Not isExistingFolder
Catch fnfEx As FileNotFoundException
'.....
End Try
通过谷歌搜索,我发现:
public bool IsFolder(string path)
{
return ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory);
}
然后可以按如下方式调用该函数:
// Define a test path
string filePath = @"C:\Test Folder\";
if (IsFolder(filePath)){
MessageBox.Show("The given path is a folder.");
}
else {
MessageBox.Show("The given path is a file.");
}
通过谷歌搜索,我发现:
public bool IsFolder(string path)
{
return ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory);
}
然后可以按如下方式调用该函数:
// Define a test path
string filePath = @"C:\Test Folder\";
if (IsFolder(filePath)){
MessageBox.Show("The given path is a folder.");
}
else {
MessageBox.Show("The given path is a file.");
}
@Erno,这是真的,但是如果OP的目的是操纵这个文件,那么它可能是一个安全检查。此外,如果给定字符串,文件和目录都不存在,则无法确定它是文件还是目录。@Darin:这取决于他从哪里获得源路径。@Erno,你是什么意思?正如我所说的,如果给定一个字符串,文件和目录都不存在,无论从何处获取,您都无法判断该字符串是否表示文件或目录。@Erno,这是真的,但如果OP的目的是操作该文件,那么可能是安全检查。此外,如果给定字符串,文件和目录都不存在,则无法确定它是文件还是目录。@Darin:这取决于他从哪里获得源路径。@Erno,你是什么意思?正如我所说的,如果给定一个字符串(无论从何处获得该字符串),文件和目录都不存在,则无法判断该字符串是否表示文件或目录。您可以在.NET 4中使该方法更短:return file.getAttributePath.HasFlagFileAttributes.directory;。您可以在.NET 4:return File.getAttributePath.HasFlagFileAttributes.Directory;中使此方法更短;。这个问题的答案是否被错误地归档了?我看不出两者之间有什么关联。这个问题的答案是否被错误地归档了?我看不出有什么关联。