0672322773
- Chapter 3: Manipulating Files
- The Windows CE TreeView Control
- Creating the PocketExplorer Utility
- Summary
Chapter 3: Manipulating Files
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 filingprobably 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.
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).
Figure 3.1 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 |
EOF |
Kill |
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.