How to: Trap System Message Dialog?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ESmith

    How to: Trap System Message Dialog?

    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.WriteAllBy tes (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


  • Peter Duniho

    #2
    Re: How to: Trap System Message Dialog?

    On Fri, 20 Jun 2008 05:03:54 -0700, ESmith <eliana_smith@h otmail.com>
    wrote:
    [...]
    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. To some extent, if checking for read-only status isn't
    suitable for your purposes, you probably should just trust the OS to deal
    with things correctly. The question as to whether an error will be
    presented to the user when a write to some device fails is really more a
    contract between the user and the OS. An application intervening or
    voiding that contract should do so only in the most unusual situations.

    Pete

    Comment

    • Jeroen Mostert

      #3
      Re: How to: Trap System Message Dialog?

      Peter Duniho wrote:
      On Fri, 20 Jun 2008 05:03:54 -0700, ESmith <eliana_smith@h otmail.com>
      wrote:
      >
      >[...]
      >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.

      --
      J.

      Comment

      Working...