Click here to Skip to main content
15,881,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was giving my systems D drive as root for the treeview node, But i am getting the operating system protected files also in the list even if i select the folder option "Dont show the hidden files,folders and drives" , I dont need the operating system protected files in my treeview. Please help me to work this out.
Posted

1 solution

Hi there,

Unfortunately the search options in listing files doesn't have that option, at least to my knowledge. I think you have to write your own method to achieve this. If you are in .net 4.0, you could use EnumerateDirectories. Try something like this :
C#
private IEnumerable<DirectoryInfo> EnumerateDirectories(DirectoryInfo dir, string target)
{
    foreach (var di in dir.EnumerateDirectories("*",SearchOption.TopDirectoryOnly))
    {
        if ((di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            if (di.Name.EndsWith(target, StringComparison.OrdinalIgnoreCase))
            {
                 yield return di;
                 continue;
            }
            foreach (var subDir in EnumerateDirectories(di, target))
            {
                yield return subDir;
            }
        }
    }
}

and then :
C#
DirectoryInfo dir = new DirectoryInfo(@"C:\RootDir");
var found = EnumerateDirectories(dir,"target").ToArray();

This way, if you run into a hidden directory, you stop going through sub directories. Be aware that it may to scan 'nested' directories though.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900