C# equivalent of powershell get-childitem -recurse while omitting certain folders

Viewed 570

I have a powershell script that I currently use when I am searching for files. I am trying to port it to c# but cannot seem to get an equivalent working.

The Powershell script is:

    $drives= gwmi Win32_LogicalDisk -Filter "DriveType=3 or DriveType=4"|select Name|foreach-object {$_.Name + "\"}


 $files=@(

  "*.txt","*.docx","*.doc"
       )



    foreach ($drive in $drives){




    $filenames = Get-ChildItem $drive -Recurse -include $files -erroraction silentlycontinue | Where {($_.FullName -notlike *Windows*) -and ($_.FullName -notlike "*Microsoft*") -and ($_.FullName -notlike "*Temp*") -and ($_.FullName -notlike "*Google*") -and ($_.FullName -notlike "*Mozilla*") -and ($_.FullName -notlike "*Recycle*") -and ($_.FullName -notlike "*tmp*")} |where-object{!($_.PSIsContainter)} | % {$_.FullName}

I have this c# code to search through directories but I am not sure how to omit directories and cannot find nay examples online

     var extensions = new List<string> { ".txt", ".doc" };
     string[] files = Directory.GetFiles(sDir, "*.*",    SearchOption.AllDirectories)
                .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray();

Thanks

1 Answers
var extensions = new HashSet<string> { @".txt", @".doc" };
var excludedFilePatterns = new HashSet<string> { @"Microsoft", @"Temp" }; // etc, etc

var files = Directory
     .GetFiles(sDir, @"*.*", SearchOption.AllDirectories)
     .Where(file => extensions.Contains(Path.GetExtension(file)))
     .Where(file =>
     {
         var fileName = Path.GetFileName(file);

         return excludedFilePatterns
             .All(excludedFilePattern =>
                 !fileName.Contains(excludedFilePattern, StringComparison.OrdinalIgnoreCase));
     })
     .ToArray();
Related