开发者

Hidden folders C# [duplicate]

开发者 https://www.devze.com 2023-01-23 23:57 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: C# - Get a list of files excluding those that are hidden
This question already has answers here: Closed 12 years ago.

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 fo开发者_如何转开发lders.

string[] folders = Directory.GetDirectories(path);


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.


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


Something like

var dirs = Directory.GetDirectories("C:").Select(dir => new DirectoryInfo(dir))
    .Where(dirInfo => (!dirInfo.Attributes.HasFlag(FileAttributes.Hidden)));
0

精彩评论

暂无评论...
验证码 换一张
取 消