c# - How to list all files and folders on a hard drive?
16,607
Solution 1
Please try using the code:
private static IEnumerable<string> Traverse(string rootDirectory)
{
IEnumerable<string> files = Enumerable.Empty<string>();
IEnumerable<string> directories = Enumerable.Empty<string>();
try
{
// The test for UnauthorizedAccessException.
var permission = new FileIOPermission(FileIOPermissionAccess.PathDiscovery, rootDirectory);
permission.Demand();
files = Directory.GetFiles(rootDirectory);
directories = Directory.GetDirectories(rootDirectory);
}
catch
{
// Ignore folder (access denied).
rootDirectory = null;
}
if (rootDirectory != null)
yield return rootDirectory;
foreach (var file in files)
{
yield return file;
}
// Recursive call for SelectMany.
var subdirectoryItems = directories.SelectMany(Traverse);
foreach (var result in subdirectoryItems)
{
yield return result;
}
}
Client code:
var paths = Traverse(@"Directory path");
File.WriteAllLines(@"File path for the list", paths);
Solution 2
Try
string[] files = Directory.GetFiles(@"C:\\", "*.*", SearchOption.AllDirectories);
This should give you a array containing all files on the hard disk.
Author by
derp_in_mouth
Updated on June 12, 2022Comments
-
derp_in_mouth almost 2 years
I want to list all files and folders that my program has access to and write them to a text file. How would I get the list? I need a way that will catch or not throw UnauthorizedAccessExceptions on folders that are not accessible.
-
derp_in_mouth over 11 yearsI cannot find a way to list ALL files without getting an UnauthorizedAccessException with this method.
-
Jonathan Wood over 11 yearsYou're going to have to give a bit of effort to work through this. Look through the methods as there are ones for getting permissions. But, in the end, you could always trap exceptions and, if you get that exception, then your program knows it cannot access that particular location.
-
Eilistraee over 11 yearsIt's because you don't have sufficient rights to access all thedirectories on the hard drive. You should simply wrap the call with try blocks and manage these errors.
-
Sergey Vyacheslavovich Brunov over 11 years@aiodintsov, of course! Because of Windows UAC. You can add the manifest for your application (codeproject.com/Articles/17968/…) to use the elevation.
-
Maate over 11 yearsPlease consider not using Directory.GetFiles() for retrieving all files on a machine. This method returns an array and thus it will do the whole iteration when you call the method. It will be hard to predict the implications on the client machine and most likely you will use much more client-side resources than you would like. I for sure would not want any code running on my pc that did this. If you really want to enumerate all files on a pc you should consider a much more gentle approach imho, starting with Directory.EnumerateFiles as suggested by @Candie :-)
-
Hooman almost 5 yearsUsing your provided solution throws
Access to the path 'C:\$Recycle.Bin\S-1-5-21-1211431010-2331069801-34201645-1008' is denied.