Detecting disk insertion and removal in the Compact Framework

I blogged about this feature of the SDF two years ago (almost to the day in fact), but it certainly bears repeating since it’s so useful.  If you need to know wehn a disk (SD, CF, USB, etc) has been attached or removed from a CE device (including Windows Mobile) you can use the SDF‘s DeviceStatusMonitor class (which, generally speaking, wraps the RequestDeviceNotifications API).  It’s really, really simple to use:

private void WatchForDrives()
  DeviceStatusMonitor monitor = new DeviceStatusMonitor(DeviceClass.FileSystem, false);
  monitor.DeviceNotification += delegate(object sender, DeviceNotificationArgs e)
    string message = string.Format(“Disk ‘{0}’ has been {1}.”, e.DeviceName, e.DeviceAttached ? “inserted” : “removed”);
    MessageBox.Show(message, “Disk Status”);

3 thoughts on “Detecting disk insertion and removal in the Compact Framework”

  1. Does this also cover devices disappearing due to power loss as well? I remember having an issue _years_ back on a HHP 750 as the SD card would drop for a number of seconds if the device went into idle. This was a problem as some of the writes and flushes were delayed. So the proc was still working but the target disk was away.


  2. It’s based on the device manager’s notifications, so as long as it gets remounted (which every device I’ve seen does during power transitions) then it should, yes.


  3. I’ve implemented this in order to detect when I connect/disconnect a usb cable from my mobile device, but i don’t get notifications of a disconnect until it is reconnected. In otherwords, I get dually notified (of both the "unmount" and "mount") when I re-attach a cable after detaching it.

    I am really just looking for the state as sending data to the port during when disconnected causes all future transmissions to fail. Any suggestions?




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s