4

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
  • 2,003
  • 4
  • 15
  • 17
  • Possible duplicate of [How to recursively list all the files in a directory in C#?](https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c) – Adam Strobel Nov 27 '18 at 19:34

5 Answers5

10

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);
  • @aiodintsov, of course! Because of Windows UAC. You can add the manifest for your application (http://www.codeproject.com/Articles/17968/Making-Your-Application-UAC-Aware) to use the elevation. – Sergey Vyacheslavovich Brunov Sep 08 '12 at 17:14
2

Try

string[] files = Directory.GetFiles(@"C:\\", "*.*", SearchOption.AllDirectories);

This should give you a array containing all files on the hard disk.

ahajib
  • 12,838
  • 29
  • 79
  • 120
heljem
  • 21
  • 1
  • 2
    Using your provided solution throws ```Access to the path 'C:\$Recycle.Bin\S-1-5-21-1211431010-2331069801-34201645-1008' is denied.``` – Hooman Limouee May 08 '19 at 17:18
0

Check out Directory.GetFiles() and related methods.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 2
    I cannot find a way to list ALL files without getting an UnauthorizedAccessException with this method. – derp_in_mouth Sep 08 '12 at 17:00
  • 1
    You'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. – Jonathan Wood Sep 08 '12 at 17:01
  • It'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. – Eilistraee Sep 08 '12 at 17:02
  • Please 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 :-) – Maate Sep 08 '12 at 17:15
0

You can try with

var files = from file in Directory.EnumerateFiles(@"your Path", "*.*", SearchOption.AllDirectories)
            select file;
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

Using Traverse that Serge suggested and the DriveInfo class you can have access to all files available to the pc.

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
   if (d.IsReady == true)
   {
      var paths = Traverse(d.Name);
      File.AppendAllLines(@"File path for the list", paths);
   }
}
athoik
  • 363
  • 4
  • 8