0

I am trying to figure out how to continue from a UnauthorizedAccessException error. I am trying to list all files in my drives and have used the try/ctch statements and continue but nothing seems to work.

Here is the code:

using System;
using System.IO;
using System.Linq;

public class Program
{
    public static void Main(string[] args) 
    {
        foreach(DriveInfo d in DriveInfo.GetDrives().Where(x = > x.IsReady))
        {
            try
            {
                string[] files = Directory.GetFiles(d.RootDirectory.FullName, @"*.*", SearchOption.AllDirectories).ToArray();

                foreach(string file in files)
                {
                    Console.Write(file);
                }
            }

            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e.Message);
                continue;
            }
        }
    }
}

The exeception catches that I cannot access 'C:\Documents and Settings' but then terminates the code instead of listing the rest of the files that I can access. I have read up and know this is a problem/bug with net but cannot find out how to continue even after catching the exception.

Daniel Lord
  • 754
  • 5
  • 18
Huyam
  • 13
  • 3
  • Please fix the indentation of your source code and [edit] your question to include the new full source code you have. Then you will see where a `try` block starts and where any exception will be handled by a `catch` block. – Progman Mar 02 '21 at 19:50
  • I have edited it to include the full source code – Huyam Mar 02 '21 at 20:26
  • But you haven't fixed the indentation of your source code. Use the features of the IDE you are using to fix the indentation of your source code and [edit] your question to include the new formatted source code you have. – Progman Mar 02 '21 at 20:27
  • You might want to check https://stackoverflow.com/questions/4942113/is-there-a-format-code-shortcut-for-visual-studio for how to format/indent your code in Visual Studio. – Progman Mar 02 '21 at 20:34
  • Ok thanks for your help. First question so good to know. Any idea on how to fix the exception problem terminating the code? – Huyam Mar 02 '21 at 20:38
  • It might help you https://stackoverflow.com/questions/65542300/how-can-i-get-all-files-in-a-directory-skipping-unauthorized-files – Svyatoslav Ryumkin Mar 02 '21 at 20:52

1 Answers1

0

You need to use recursion to query one directory at a time. This way you can catch any unauthorized exceptions and continue your search.

static void Main()
{
    foreach (DriveInfo info in DriveInfo.GetDrives().Where(c => c.IsReady))
    {
        foreach (string file in DirSearch(info.RootDirectory.FullName))
        {
            Console.WriteLine(file);
        }
    }
    Console.ReadKey(true);
}

private static IList<string> DirSearch(string path)
{
    List<string> files = new List<string>();
    try
    {
        foreach (string dir in Directory.GetDirectories(path))
        {
            foreach (string file in Directory.GetFiles(dir))
            {
                files.Add(file);
            }
            files.AddRange(DirSearch(dir));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return files;
}
mr.coffee
  • 962
  • 8
  • 22
  • Thanks for your answer. How would I change this so I have a list of files like in my original question as I plan to do some filtering once I have this working i.e using .Where(file => extensions.Contains(Path.GetExtension(file))) – Huyam Mar 02 '21 at 20:56
  • I have updated my answer to return a List a files – mr.coffee Mar 02 '21 at 21:04