Manipulating Files CHAPTER 3
Total Page:16
File Type:pdf, Size:1020Kb
04 2773 Ch03 10/19/01 12:24 PM Page 33 Manipulating Files CHAPTER 3 IN THIS CHAPTER • The FileSystem and File Controls 35 • The Windows CE TreeView Control 36 • Creating the PocketExplorer Utility 37 04 2773 Ch03 10/19/01 12:24 PM Page 34 eMbedded Visual Basic: Windows CE and Pocket PC Mobile Applications 34 Most Visual Basic programmers today access files through the familiar Win32 File System Object (commonly called the fso). The fso provides an easy-to-use and quick-to-develop-with object model that works for most text file applications. When it comes to binary or random access, the fso falters, and VB developers then turn to what’s often called free filing—probably named as such due to using the FreeFile() method to get an available file number. Free filing is an older, procedural-based method of access files that’s valid for any file access, but isn’t quite as easy to use as the object-oriented fso. Although the eVB’s FileSystem and File controls don’t have the extensive object model that the fso has, they provide just about everything developers could want or need for accessing any type of file-binary or text, sequential or random. In fact, I find that using these controls has distinct advantages over file programming in Win32 because they wrap the functionality of both free filing and the fso in a nice, compact set of methods and properties. Whether accessing small text configuration files or large binary data files, the FileSystem and File controls are all you’ll need. This chapter explores some of the functionality behind the two controls in the MSCEFileCtl library: FileSystem and File. The controls’ uses are limited only by the applications in which they are used and by your coding imagination; an exhaustive treatment of all the entire library’s uses could be a book in itself. We’ll cover some of their more common uses, give you ideas on where else you might use them, and add in the use of the TreeView control as well. In this chapter you will • Learn about the object models for the FileSystem and File controls • Discover the differences between the FileControl functions and Visual Basic’s well- known File System Object (fso) • Learn about the object model for the native CE TreeView control • Read and traverse a directory tree • Use the Open, Read, Write, and Close text files • See how to retrieve file attributes, such as size and modify date • Examine how to minimize the impact of the CreateObject memory leak • Work around the lack of support for properties As we’ll do with all chapters, we will also uncover some of eVB’s more subtle nuances that can cause problems and headaches, as well as give coding tips applicable to any eVB application. 04 2773 Ch03 10/19/01 12:24 PM Page 35 Manipulating Files 35 CHAPTER 3 The FileSystem and File Controls The Microsoft FileSystem and File controls are distributed with the platform SDK for each CE platform. They are both included in the MSCEFile.dll library. Controls are added to projects in eVB in the same manner as in other versions of Visual Basic (by choosing Components from the Tools menu). You can add the File and FileSystem controls specifically to your project via the Microsoft CE FileSystem Control 3.0 component library (see Figure 3.1). 3 M ANIPULATING F FIGURE 3.1 ILES Adding the FileSystem control library to your project. Adding the reference places both controls into the eVB toolbox. The File control’s button shows a small file icon, whereas the FileSystem control shows a small two-drawer filing cabinet (see Figure 3.2). The Controls’ Object Models This chapter will cover a large amount of the File and FileSystem controls’ object models. Covering the entire object model for any library in a single project is difficult and often impractical. Many methods are used in just about every project, whereas others are used only for specific, narrowly focused uses. The following is a list of the methods and properties used in this chapter: File Control FileSystem Control Close Dir Input FileDateTime LinePrint FileLen Open GetAttr EOFKill 04 2773 Ch03 10/19/01 12:24 PM Page 36 eMbedded Visual Basic: Windows CE and Pocket PC Mobile Applications 36 File control FileSystem control FIGURE 3.2 The eVB toolbox with the standard controls, the File control and the FileSystem control. As a reference guide, Appendix A includes all the object models for the File and FileSystem controls. The Windows CE TreeView Control The application developed in this chapter provides a pretty good look at the Windows CE ver- sion of the familiar TreeView control. As you can expect, the object model is smaller and the features are fewer than what you get with the Win32 TreeView control, but it’s sufficiently robust to provide all the common functionality TreeViews are used for without having to hunt for or write your own custom control. The following is a list of the TreeView control’s methods and properties used in this chapter: Close Input LinePrint Open EOF Dir 04 2773 Ch03 10/19/01 12:24 PM Page 37 Manipulating Files 37 CHAPTER 3 FileDateTime FileLen GetAttr Kill Again, I feel it’s useful to have a full object model reference to be able to refer to in not only this project, but for any other project you work on as well. Appendix A lists the full object model for the Windows CE TreeView control. The TreeView is in its own control library, MSCETreeView.dll, and, just like the FileSystem and File controls, can be added to a project’s toolbox through the Project Components dialog. Simply select Microsoft Windows CE Treeview Control 3.0. Creating the PocketExplorer Utility The best way to familiarize yourself with the FileSystem and File controls is to dive in and begin using them for a meaningful purpose. To do that, we’re going to write a small utility application called PocketExplorer that’s somewhat of a mix between the standard Windows Explorer and Notepad. 3 PocketExplorer will allow you to navigate through all the folders on your PocketPC as well as M ANIPULATING view the files within the folders and their attributes. It will also allow you to open and edit any F file, though only in text mode. ILES Setting Up the Project PocketExplorer uses two forms—frmMain and frmView—and one module, modMain. Listings 3.1 and 3.2 contain all the heading information for frmMain and frmView, respec- tively. For clarity, I have included listings only for properties that I modified from their defaults. Figure 3.3 shows what my project looks like. LISTING 3.1 Heading Information from frmMain.ebf Begin VB.Form frmMain Caption = “PocketExplorer” ClientHeight = 4005 ClientLeft = 60 ClientTop = 840 ClientWidth = 3630 ShowOK = -1 ‘True Begin FILECTLCtl.FileSystem fsMain Left = 2220 04 2773 Ch03 10/19/01 12:24 PM Page 38 eMbedded Visual Basic: Windows CE and Pocket PC Mobile Applications 38 LISTING 3.1 Continued Top = 180 _cx = 2200 _cy = 1400 End Begin MSCETREEVIEWLibCtl.TreeViewCtl tvwFolders Height = 1755 Left = 30 Top = 0 Width = 3555 LabelEdit = 1 LineStyle = 1 PathSeparator = “/” Style = 6 End Begin VBCE.Label lblFiles Height = 255 Left = 2160 Top = 3720 Width = 495 Caption = “Files:” Alignment = 1 End Begin VBCE.CommandButton cmdDelete Height = 315 Left = 1380 Top = 3660 Width = 615 Caption = “Del” End Begin VBCE.CommandButton cmdNew Height = 315 Left = 720 Top = 3660 Width = 615 Caption = “New” End Begin VBCE.ComboBox cboFilter Height = 300 Left = 2700 Top = 3660 Width = 855 Text = “” End Begin VBCE.CommandButton cmdEdit 04 2773 Ch03 10/19/01 12:24 PM Page 39 Manipulating Files 39 CHAPTER 3 LISTING 3.1 Continued Height = 315 Left = 60 Top = 3660 Width = 615 Caption = “Edit” Enabled = 0 ‘False End Begin VBCE.ListBox lstFiles Height = 1785 Left = 30 Top = 1800 Width = 3555 End End NOTE Listings 3.1 and 3.2 don’t show the entire headings, but show all control settings that 3 I’ve changed from their defaults. M ANIPULATING F ILES LISTING 3.2 Heading Information from frmView.ebf Begin VB.Form frmView Caption = “FileName” ClientHeight = 4005 ClientLeft = 60 ClientTop = 345 ClientWidth = 3630 ShowOK = -1 ‘True Begin VBCE.CommandButton cmdSave Height = 375 Left = 2400 Top = 3600 Width = 1035 Caption = “Save” End Begin VBCE.TextBox txtView Height = 3495 Left = 30 Top = 30 Width = 3555 04 2773 Ch03 10/19/01 12:24 PM Page 40 eMbedded Visual Basic: Windows CE and Pocket PC Mobile Applications 40 LISTING 3.2 Continued Text = “” MultiLine = -1 ‘True ScrollBars = 3 End End FIGURE 3.3 The final Form layouts for PocketExplorer. Getting Directory Contents One primary function of PocketExplorer is to display directory contents, so you need to write a workhorse function that can be called to do so. You want the function to read a directory, and then populate the TreeView with any subfolders in the directory as well as populate the textbox with all the directory files, provided they meet your filter conditions. To make the code a little easier to reuse in other projects, it’s a good idea to make it a function that’s entirely independent of this project. That means you should have no direct references to any of the forms’ controls, so what you need is a function that will take all this information as input parameters. 04 2773 Ch03 10/19/01 12:24 PM Page 41 Manipulating Files 41 CHAPTER 3 Call the ReadDirectory function and put it in the code module modMain. Public Sub ReadDirectory(Path As String, _ fsFileSystem As FILECTLCtl.FileSystem, _ tvwTreeView As MSCETREEVIEWLibCtl.TreeViewCtl, _ nodParentNode As MSCETREEVIEWLibCtl.Node, _ lstListBox As ListBox, _ strFilter As String) When giving the parameter types, it’s important to provide the libraries they come from (that is, FILECTLClt.FileSystem instead of just FileSystem) because without them, you won’t get IntelliSense for the object inside the function.