Catching exceptions during DirectoryInfo.GetFiles?

  • Thread starter Thread starter Olaf Rabbachin
  • Start date Start date
O

Olaf Rabbachin

Hi folks,

I'm copying files from i.e. a memory-card to the hard-drive.
The code roughly looks like this abstract:

Dim di As New DirectoryInfo("E:\")
Dim fi As FileInfo
Try
For Each fi In di.GetFiles
'...
Next
Catch ...
End Try

I would've expected that the above code would catch all exceptions,
including i.e. the one thrown if the user retracts the memory-card during
the process. However, if the disk is removed during di.GetFiles I'll get a
standard messagebox "There is no disk in the drive. Please insert a disk
into drive \Device\Harddisk1\DR3" and the Catch-block will be executed
after the user clicked any of the messagebox's buttons.
That's exactly what shouldn't ever happen.

Any suggestions?

TIA & cheers,
Olaf
 
Any suggestions?

You can get rid of the dialog box by calling the Win32 API
SetErrorMode. I don't think there's any managed equivalent for that.



Mattias
 
Hi,
You can get rid of the dialog box by calling the Win32 API
SetErrorMode. I don't think there's any managed equivalent for that.

thanks.
But the whole problem really gives me the creeps in general as the
existance of that behaviour really renders try/catch useless if there's
(fundamental!) functions within the CLR that implement their own
error-handling without the chance to have the caller take care of that.

Cheers,
Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top