Home All Groups Group Topic Archive Search About

Catching exceptions during DirectoryInfo.GetFiles?

Author
6 Apr 2005 8:40 AM
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

Author
6 Apr 2005 12:05 PM
Mattias Sjögren
>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

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
7 Apr 2005 8:03 AM
Olaf Rabbachin
Hi,

Mattias Sjögren wrote:

>>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.

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