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.