Browse DevX
Sign up for e-mail newsletters from DevX


Build a Souped Up File Explorer for Your PDA : Page 3

Replace the lackluster File Explorer that comes with the Pocket PC with one that better mimics Windows Explorer and learn to manipulate the .NET Compact Framework's TreeView control in the exchange.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Adding User Functions
The pathname used in Windows CE is not the same as that in Windows. In Windows CE, the root directory starts with a \ and subsequent subdirectories are separated by a \. For example, \Program Files indicates that the root contains a subdirectory named Program Files. The FullPath property of the Node object returns the full path name of a node, starting from the root node. The path is delimited by the \ character.

There is a minor problem you have compensate for. Because the root node is indicated by a \, a typical path will contain an extra \, for example \\Program Files. Hence there is a need to remove the extra \ in the path name using the stripExtraSlash() method:

Public Function stripExtraSlash(ByVal str As String) As String ' ---strip away the extra "\" for ' subdirectories. e.g. \\My documents Dim path As String If str.Length > 1 And (Mid(str, 1, 1) = "\") Then path = Mid(str, 2, str.Length - 1) Else path = str End If Return path End Function

When the user taps and holds an item with the stylus, which is equivalent to a right-click with a mouse, the context menu will be shown. You need to program a way for the File Explorer to handle common user functions such as Copy and Cut. If the user chooses Copy or Cut, the filename as well as the path of the item to be copied will be saved. When using cut, the selected node will also be removed:

Private Sub mnuCopy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuCopy.Click ' ---copy a file--- pathName = stripExtraSlash(TreeView1.SelectedNode.FullPath) fileName = TreeView1.SelectedNode.Text copy = True mnuPaste.Enabled = True End Sub Private Sub mnuCut_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuCut.Click ' ---cut a file--- pathName = stripExtraSlash(TreeView1.SelectedNode.FullPath) fileName = TreeView1.SelectedNode.Text copy = False mnuPaste.Enabled = True ' ---remove the node--- TreeView1.SelectedNode.Remove() End Sub

To delete an item from the Pocket PC, use the File.Delete() method from the System.IO namespace. You must also remove the node representing the item to be deleted:

Private Sub mnuDelete_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuDelete.Click ' ---delete a file--- File.Delete(stripExtraSlash(TreeView1.SelectedNode.FullPath)) TreeView1.SelectedNode.Remove() End Sub

To allow the user to paste an item that has just been copied or cut, use the File.Copy() method. You also need to add a new node to the TreeView control:

Private Sub mnuPaste_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuPaste.Click ' ---paste a file--- File.Copy(pathName, stripExtraSlash _ (TreeView1.SelectedNode.FullPath) & _ "\" & fileName, True) ' ---add the new node to the tree--- Dim node As New System.Windows.Forms.TreeNode node.Text = fileName node.ImageIndex = icoFile node.SelectedImageIndex = icoFile TreeView1.SelectedNode.Nodes.Add(node) ' ---if cut operation, then delete file--- If Not copy Then File.Delete(pathName) End If mnuPaste.Enabled = False End Sub

With that, you have a simply but fully functional mobile File Explorer that should look and feel like Windows Explorer on a desktop. You've created a TreeView control to allow the user to browse the file structure hierarchically, including the display of icons; you've allowed for expanding and collapsing directories; as well as supported copy, cut, and paste commands using methods from the System.IO namespace.

Figure 5. Start Browsing: Be sure to test the completed enhanced File Explorer application.
To debug the application, press F5. The completed application will look like Figure 5.

In the third and final article in this series, I'll show you how to create controls during runtime. You will also learn how to develop .NET CF applications without using Visual Studio .NET.

Wei-Meng Lee is a Microsoft .NET MVP and co-founder of Active Developer, a technology company specializing in hands-on training on the latest technologies. He is an established developer and trainer specializing in .NET and wireless technologies. He speaks and writes frequently on topics ranging from .NET to Mac OS X. He is the author of "Windows XP Unwired" (OReilly & Associates) and is currently working on "Programming the .NET Compact Framework," also from OReilly. He can be reached at weimeng@activedevelop.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date