【问题标题】:How to determine whether an object is a file or a folder如何判断一个对象是文件还是文件夹
【发布时间】:2011-05-14 21:17:11
【问题描述】:

我正在尝试确定给定路径是指向文件还是目录。目前我的逻辑很简单,涉及以下检查

if (sourcePath.Contains(".")) // then treat this path as a file 

上面的问题是文件夹名称中也可以有句点。我希望能够确定给定路径是文件的路径,而不必尝试实例化文件流类型并尝试访问它或类似的东西。

有没有办法做到这一点?

提前致谢

【问题讨论】:

    标签: c# file file-io filepath


    【解决方案1】:

    您可以使用File.Exists 方法:

    如果路径描述了一个目录,这 方法返回false

    所以:

    if (File.Exists(sourcePath))
    {
        // then treat this path as a file
    }
    

    还有Directory.Exists 方法,文档中给出了以下示例:

    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);
    } 
    

    【讨论】:

    • @Erno,这是真的,但如果 OP 的意图是操纵这个文件,那么它可能是一个安全检查。此外,如果给定字符串,文件和目录都不存在,则无法判断它是文件还是目录。
    • @Darin:这取决于他从哪里获得 sourcePath。
    • @Erno,你是什么意思?正如我所说,如果文件和目录都不存在给定一个字符串(无论你从哪里得到它),你就无法判断这个字符串是代表文件还是目录。
    【解决方案2】:

    C#:

    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
    

    如果文件不存在,此函数将抛出File not found exception。所以你必须抓住它(或使用 Darin Dimitrow 的方法)。

    Try
        Dim isExistingFolder As Boolean = IsFolder(path)
        Dim isExistingFile = Not isExistingFolder 
    Catch fnfEx As FileNotFoundException
        '.....
    End Try 
    

    【讨论】:

    • 您可以在 .NET 4 中使此方法更短:return File.GetAttributes(path).HasFlag(FileAttributes.Directory);
    【解决方案3】:
    var isDirectory = (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
    

    【讨论】:

      【解决方案4】:

      System.IO.File.Exists("yourfilenamehere") 可以解决问题。如果路径不是文件,这将返回 false。如果路径不存在,它也会返回 false,所以要小心。

      【讨论】:

        【解决方案5】:

        通过谷歌搜索,我发现了这个:

        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.");
        }
        

        【讨论】:

          【解决方案6】:
          List<string> RtnList = new List<string>();
          foreach (string Line in ListDetails)
          {
              if (line.StartsWith("d") && !line.EndsWith("."))
              {
                  RtnList.Add(line.Split(' ')[line.Split(' ').Length - 1] );
          
          
              }
          }
          

          【讨论】:

          • 这个答案是否被错误地提交给了这个问题?我没有看到相关性。
          猜你喜欢
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多