Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ths issue with the above answer is that is does not take care of exception in sub directories. This would be a better way to handling those exceptions so you get ALL files from ALL subdirectories except those with threw an access exception:</p> <pre><code> /// &lt;summary&gt; /// A safe way to get all the files in a directory and sub directory without crashing on UnauthorizedException or PathTooLongException /// &lt;/summary&gt; /// &lt;param name="rootPath"&gt;Starting directory&lt;/param&gt; /// &lt;param name="patternMatch"&gt;Filename pattern match&lt;/param&gt; /// &lt;param name="searchOption"&gt;Search subdirectories or only top level directory for files&lt;/param&gt; /// &lt;returns&gt;List of files&lt;/returns&gt; public static IEnumerable&lt;string&gt; GetDirectoryFiles(string rootPath, string patternMatch, SearchOption searchOption) { var foundFiles = Enumerable.Empty&lt;string&gt;(); if (searchOption == SearchOption.AllDirectories) { try { IEnumerable&lt;string&gt; subDirs = Directory.EnumerateDirectories(rootPath); foreach (string dir in subDirs) { foundFiles = foundFiles.Concat(GetDirectoryFiles(dir, patternMatch, searchOption)); // Add files in subdirectories recursively to the list } } catch (UnauthorizedAccessException) { } catch (PathTooLongException) {} } try { foundFiles = foundFiles.Concat(Directory.EnumerateFiles(rootPath, patternMatch)); // Add files from the current directory } catch (UnauthorizedAccessException) { } return foundFiles; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload