C # search using directory class

I am writing a software tool that, as part of its main task, is to search for a directory and its auxiliary directories for a directory with a given name and save each path to a file that ends with the specified directory name in a string array, For example:

                   level_1 level_2 Level_3
RootDirectory ---> folderA ---> folderD ---> FolderF ---> Target
             | | ---> folderE ---> Target
             |            
             | ---> folderB ---> Target
             |
             | ---> FolderC ---> Target

should pump out:

string[] = {RootDirectory\folderA\FolderD\folderF\Target,  
    RootDirectory\folderA\folderE\Target, 
    Rootdirectory\folderB\Target, 
    RootDirectory\foderC\Target}

I originally used getDirectories(myPath, "Target", SearchOption.AllDirectories)catalog information for the object, but there was a problem. For some reason, it will find the target under folders b and c, as well as in folder A> folderD> folderF, but will skip FolderE. When he finds the first entry in the subdirectory folderA, he will move to the next folder at level_1. I should mention that folderD in my real case is actually sorted alphabetically before folderE, as in this example

so instead, I decided to use IEnumeratorand run the where filter to select files that end with the name of this directory. It found them all. However, I cannot figure out how to do something like getDirectories().Where(x=>(x.attributes & fileattributes.hidden)==0);on IEnumerator.

The problem is that I need to skip hidden SVN directories, because it slows down the process significantly.

, : , , ?

+3
2

, . .net 4.0, EnumerateDirectories

:

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

:

DirectoryInfo dir = new DirectoryInfo(@"C:\RootDirectory");
var found = EnumerateDirectories(dir,"target").ToArray();

, "", , .

0

C:\ , Linqpad.

string root = "c:\\folderA";
string target = "Target";   
var d = new DirectoryInfo(root);
var x = d.EnumerateDirectories(target, SearchOption.AllDirectories);
x.ToList() // for each element, it in the the FullName 

- .svn

+1

All Articles