Created
March 7, 2021 02:18
-
-
Save protectroot-com/7cad1c7b8a7758e979b14b6ecc02a2ed to your computer and use it in GitHub Desktop.
ListAllFilesOnNonOSDriveFast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
//source..https://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure | |
namespace whatever | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var results = GetAllAccessibleFiles(@"d:\"); //choose your drive letter and root directory | |
for (int i=0;i<results.Count;i++) | |
{ | |
Console.WriteLine(results[i]); | |
} | |
} | |
public static List<string> GetAllAccessibleFiles(string rootPath, List<string> alreadyFound = null) | |
{ | |
if (alreadyFound == null) | |
alreadyFound = new List<string>(); | |
DirectoryInfo di = new DirectoryInfo(rootPath); | |
var dirs = di.EnumerateDirectories(); | |
foreach (DirectoryInfo dir in dirs) | |
{ | |
if (!((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) | |
{ | |
alreadyFound = GetAllAccessibleFiles(dir.FullName, alreadyFound); | |
} | |
} | |
var files = Directory.GetFiles(rootPath); | |
foreach (string s in files) | |
{ | |
alreadyFound.Add(s); | |
} | |
return alreadyFound; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment