1

I was wondering if there is a way to solve the issue I have with this code:

DriveInfo[] dDrives = DriveInfo.GetDrives();
foreach(DriveInfo dDrive in dDrives)
{
    try
    {
        string sDrive = dDrive.ToString();
        string[] sSearch = Directory.GetFiles(sDrive, sFile, SearchOption.AllDirectories);
        foreach(string sResult in sSearch)
        {
            textBox2.Text = sResult + Environment.NewLine;
        }
    }
    catch
    {
    }
}

When it comes across a file that isn't accessible because of permissions, it will goto the catch and end. What I need it to do is if it comes across a file it can't access, go back to the try block and continue searching. Any help is much appreciated, thanks!

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • See also [How can you easily check if access is denied for a file in .NET?](http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net) – Justin Jul 20 '11 at 13:01
  • See also: http://stackoverflow.com/q/1393178/15667 – xan Jul 20 '11 at 14:39

2 Answers2

1

The code you have should work, though you really should rethink your logic, using exceptions for flow control is plain wrong.

This will work, though not best practice:

string sDrive = dDrive.ToString();
try
{
   string[] sSearch = Directory.GetFiles(sDrive, sFile, SearchOption.AllDirectories);
}
catch {}

foreach(string sResult in sSearch)
{
    textBox2.Text = sResult + Environment.NewLine;
}

Where exactly are you accessing files? I only see a call to list file names (Directory.GetFiles), and listing of the file names returned.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Yes, thanks, what would be the better alternative for using exceptions? – Bali C Jul 20 '11 at 12:54
  • @Bali - Where exactly are you accessing the files in your code? – Oded Jul 20 '11 at 12:56
  • I presumed it was here: string[] sSearch = Directory.GetFiles(sDrive, sFile, SearchOption.AllDirectories); – Bali C Jul 20 '11 at 13:00
  • @Bali - instead of disposing of the exception, log it to see exactly what is going on. – Oded Jul 20 '11 at 13:00
  • The error VS gives me is "Access to the path 'C:\C:\$Recycle.Bin\S-1-5-21-232317210-4223013802-3689919456-1000\ is denied" on the line string[] sSearch = Directory.GetFiles(sDrive, sFile, SearchOption.AllDirectories); – Bali C Jul 20 '11 at 13:11
  • @Bali - Do you really have a path that starts with `C:\C:\` ? – Oded Jul 20 '11 at 13:22
  • Whoops, no, I started typing it and then just copied and pasted over it, must have left the C:\ at the start :), any idea's on how to return to the foreach loop after the exception? – Bali C Jul 20 '11 at 13:30
  • The first one, just so when the exception is thrown then it can return to the get files line and search again, would there be a way of just continuing if it can't access something or exclude it so it doesn't end up catching on the same one all the time? – Bali C Jul 20 '11 at 13:44
  • @Bali - You will need a `try{}catch{}` around `Directory.GetFiles`. – Oded Jul 20 '11 at 13:46
  • That is whilst it is on the same drive, I don't want it to catch, then go back to the foreach loop and start on the next drive - is this possible? Sorry I know I am wording this pretty badly but hopefully you know what I mean! – Bali C Jul 20 '11 at 13:47
  • @Oded let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1661/discussion-between-bali-c-and-oded) – Bali C Jul 20 '11 at 13:47
1

EDIT: Removed original answer as incorrect.

See: UnauthorizedAccessException cannot resolve Directory.GetFiles failure for some suggested resolutions to your problem.

Community
  • 1
  • 1
xan
  • 7,440
  • 8
  • 43
  • 65