0

I want to display all the .txt files on my system. But when I execute my code I only get displayed the .txt files on my removable Disk Drive (USB-Stick).

How can I fix this?

Thank you for your help :D

Here's my code:

static List<string> Search()
{
    var files = new List<string>();

    foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady))
    {
        try
        {
            files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories));
            Console.Write(string.Join(System.Environment.NewLine, files));
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    return files;
}

My console output:

The access to the path "C: \$ Recycling. Bin\S-1-5-18" was refused.
D:\Test\Test1.txt
D:\Test\Test2.txt
...
D:\PortableApps\EclipsePortable\Other\Source\Readme.txtSystem.Collections.Generic.List`1[System.String]

I don't get any error messages at all.

Vuapsti
  • 15
  • 5
  • Do you get some kind of error? If so, post the details of it. – Jim Mischel Dec 27 '16 at 20:44
  • Sorry I should have posted my console output^^ Going to edit my post – Vuapsti Dec 27 '16 at 20:47
  • I get the same error. Running as admin passes but then an error on c:\documents and settings. it might be best just skip those directories. – bwoogie Dec 27 '16 at 20:52
  • What do you mean "I don't get any error messages at all"? The first line is an error message. Something caught the exception and displayed an error message. – Jim Mischel Dec 27 '16 at 21:02
  • I don't see this as a error message because it just tells me that I don't have permissions for the directory. – Vuapsti Dec 27 '16 at 21:33

1 Answers1

0

It seems that GetFiles function cannot access to some file system due to security reason. Please refer to this solution: Access to all file in C drive

Community
  • 1
  • 1
popsiporkkanaa
  • 678
  • 1
  • 9
  • 17