Usher
Usher

Reputation: 2146

How to check the Sub Dir's

Am using M.Babcock code get the file list based on the "pattern" of the file name but the code below does the job but its not lookin the files in sub directories. Any help how to look the file patterns in Sub Dir's also

I know if use the below code will do the job

DirectoryInfo[] DI = new DirectoryInfo(rootdir).GetDirectories("*.*", SearchOption.AllDirectories ) ;

But i couldn't understand how can i change his code to look the files in sub dir's also

M.Babcock code

var getfiles = new fileshare.Program();
string realname = "*main*";
string Location = "SVR01";
var fileList = getFiles.GetFileList(realname, Location);
var anymatch = fileList.Any();

if (anymatch) // Or possibly `if (fileList.Any())` if anymatch isn't 
              // really used anywhere else
    baseMeta();

foreach (var file in getfiles.GetFileList(realname,Location))
    getfiles.copytolocal(file.FullName);
 switch (Location)
    {
        case "SVR01":
            directory = @"\\SVR01\Dev";
        break;

        case "SVR02":
            directory = @"\\SVR02\Dev";
        break;

        case "SVR03":
            directory = @"\\SVR03\Prod");
        break;

        default: 
            throw new ArgumentOutOfRangeException();

    }

    DirectoryInfo di = null;
    try
    {
        di = new DirectoryInfo(directory);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        yield break;
    }

    foreach(var fi in di.EnumerateFiles(pattern))
        yield return fi;
}

Thanks in Advance

Upvotes: 0

Views: 55

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You could provide the same SearchOption argument to the EnumerateFiles method that you are using:

foreach(var fi in di.EnumerateFiles(pattern, SearchOption.AllDirectories))
    yield return fi;

Upvotes: 2

Related Questions