How to select only hidden folder from String[]?

前端 未结 2 1352
长情又很酷
长情又很酷 2021-01-28 09:48

I want to know how to get only hidden folder from String[]. Actually I have one string array and there show some files. There have normal and hidden files also but I want to try

相关标签:
2条回答
  • 2021-01-28 10:30

    You can test whether a directory is hidden by checking Attributes property of the DirectoryInfo class:

    var info = new DirectoryInfo(path);
    var hidden = info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden
    

    The same code would work for testing whether a file is hidden, but you'd use FileInfo instead of DirectoryInfo. It is not clear what your array contains and what do you want to get, but generally, you could use LINQ to implement filtering. The following returns a new collection containing only hidden directories:

    var hiddenDirectories = allDirectories.Where(path => {
      var info = new DirectoryInfo(path);
      var hidden = (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden
      return hidden; });
    

    You should be able to adapt this to your needs (depending on what files/directories you want to get as the result).

    0 讨论(0)
  • 2021-01-28 10:32

    Create FileInfo object for each file and the use its Directory property to get a DirectoryInfo instance on which you can check the Attributes property that will tell you if the directory is hidden.

    0 讨论(0)
提交回复
热议问题