How to: Trap System Message Dialog?

E

ESmith

I want to be able to catch the exception thrown when trying to write to a
"locked" Usb drive.

For instance:

private void WriteToUsbDrive (string filePath, byte[] theBytes)
{
...
try
{
File.WriteAllBytes (filePath, theBytes);
}
catch (Exception ex) // Just for the example, assume only IO errors
occurred because the key is locked
{
MessageBox.Show ("Unlock the key!!!");
}
}


Testing on Vista, the system dialog will popup first telling the user that
the drive is locked, after clicking away that dialog, my dialog will then
appear - how can I prevent the OS system's dialog from appearing?

TIA
 
J

Jeroen Mostert

Peter said:
[...]
Testing on Vista, the system dialog will popup first telling the user
that
the drive is locked, after clicking away that dialog, my dialog will then
appear - how can I prevent the OS system's dialog from appearing?

I haven't tried this specifically with a "locked USB drive", but I
suspect that "locking" the drive just makes it read-only. So you should
be able to just check for it being read-only before you try to write to it.

If I recall correctly, there's an unmanaged API that controls whether
Windows presents an error message before failing an operation, but I
haven't used it since Windows 95. I'm not sure it'd even still be
supported.

SetErrorMode(). And yes, it still works fine. Though whether any SEM_ flag
has an effect on these particular dialog boxes is an open question.
 

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

Top