RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using the Smart Device Framework for Windows Mobile Development : Page 4

The .NET Compact Framework's class libraries are sadly inadequate for developing full-featured Windows Mobile applications, sometimes forcing you to rely upon the problemtatic Platform Invoke. Enter Smart Device Framework (SDF), a group of extensions designed to pick up where the Compact Framework's class libraries leave off.

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

First, declare a member variable of type DeviceStatusMonitor:

    Dim WithEvents DiskMonitor As New OpenNETCF.Net.DeviceStatusMonitor( _
       OpenNETCF.Net.DeviceStatusMonitor.FATFS_MOUNT_GUID, True)
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) _
       Handles DiskMonitor.DeviceNotification
        MsgBox("Device Name: " & e.DeviceName & ". Attached: " & _
    End Sub
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:

        fileContents = OpenNETCF.IO.FileHelper.ReadAllText( _ 
           "C:\Test.txt", System.Text.Encoding.ASCII)
Goodbye Platform Invokes!
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!

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date