2

Possible Duplicate:
C# - Get a list of files excluding those that are hidden

How do i make sure that i only get the folders that are NOT hidden?

this is what i do know but it returns all folders.

string[] folders = Directory.GetDirectories(path);
Community
  • 1
  • 1
Kimtho6
  • 6,154
  • 9
  • 40
  • 56

3 Answers3

11

You can use DirectoryInfo to check if a folder is hidden:

string[] folders = Directory.GetDirectories(path);
foreach (string subFolder in folders) {
 string folder = Path.Combine(path, subFolder);
 DirectoryInfo info = new DirectoryInfo(folder);
 if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
  // do something with your non-hidden folder here
 }
}

Another solution would be the following one-liner:

var folders = new DirectoryInfo(path).GetDirectories().Where(x => (x.Attributes & FileAttributes.Hidden) == 0);

In this case folders is an IEnumberable<DirectoryInfo>. If you want files instead of directories, simply replace GetDirectories with GetFiles.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
5

You would need to loop the directories and check the ( attrib utes ) for that directory or file.

Example:

foreach (DirectoryInfo Dir in Directory.GetDirectories(path))
{
    if (!Dir.Attributes.HasFlag(FileAttributes.Hidden))
    {
        //Add to List<DirectoryInfo>
    }
}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
3

Something like

var dirs = Directory.GetDirectories("C:").Select(dir => new DirectoryInfo(dir))
    .Where(dirInfo => (!dirInfo.Attributes.HasFlag(FileAttributes.Hidden)));
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198