Detecting the Status of Removable Storage Devices
Most Windows Mobile devices support external storage, such as Compact Flash, SD cards, etc. One common request is how do you detect whether the user has inserted (or removed) one? You can do so via the OpenNETCF.Net.dll
First, declare a member variable of type DeviceStatusMonitor:
Dim WithEvents DiskMonitor As New OpenNETCF.Net.DeviceStatusMonitor( _
Here, you indicate the type of file systems you want to monitor. Possible types of monitors are:
- BLOCK_DRIVER_GUID: This fires when the block driver for a Storage Manager device is loaded.
- CDDA_MOUNT_GUID: This fires when a CDDA file system is loaded.
- CDFS_MOUNT_GUID: This fires when a CDFS file system is loaded.
- FATFS_MOUNT_GUID: This fires when a FAT file system is loaded for a device.
- STORE_MOUNT_GUID: This fires when the store, managed by the Storage Manager, is ready.
- UDFS_MOUNT_GUID: This fires when a UDFS file system is loaded.
The second parameter of the DeviceStatusMonitor
class indicates if you should receive notifications for devices that are currently attached before you start the monitoring.
To start the monitoring, call the StartStatusMonitoring() method:
Whenever a storage card is inserted into the device, the DeviceNotification
event will fire. Hence, you just need to service this event, like this:
Private Sub DiskMonitor_DeviceNotification( _
ByVal sender As Object, _
ByVal e As OpenNETCF.Net.DeviceNotificationArgs) _
MsgBox("Device Name: " & e.DeviceName & ". Attached: " & _
Here, you can retrieve the name of the storage card as well as indicate whether it is attached (inserted) or not (removed). Figure 4
shows the prompt when you remove a SD card from the Windows Mobile device.
|Figure 4. The Prompt: This shows when a SD card is removed from the Windows Mobile device.|
Reading Strings from Text Files
One common task in the Windows Mobile development is reading contents from a text file into a string variable. Unlike the desktop version of the .NET framework, you cannot use the My namespace, like this:
'---does not work as the My namespace is not supported in .NET CF---
fileContents = My.Computer.FileSystem.ReadAllText("C:\Test.txt")
You need to use a StreamReader
class to read from the file, like this (which is a little lengthy):
Dim s As New System.IO.StreamReader("C:\Test.txt")
Dim fileContents As String = s.ReadToEnd()
To simplify this task, you can use the ReadAllText()
method from the FileHelper
class located in the OpenNetCF.dll
library, like this:
Goodbye Platform Invokes!
fileContents = OpenNETCF.IO.FileHelper.ReadAllText( _
Now you're familiar with some of the classes in the Smart Device Framework and how they help solve your programming problems for which you'd usually perform Platform Invokes
. Why not download the framework today and explore the wealth of classes contained within it? You will be grateful you did!