Files have always been an important aspect of programming. We use files to store data, and in many cases we have to manipulate files and folders from within applications. I do not need to give examples: Just about any application that allows user input must store its data to a file (or multiple files) for later retrieval — databases excluded, of course.
Manipulating files and folders is quite common, too. Organizing files into folders and processing files en masse are two typical examples. I recently ran into a few web-related tasks that are worth mentioning here. A program for placing watermarks on pictures was the first. A watermark is a graphic that’s placed over an image to indicate its origin. The watermark is transparent, so it doesn’t obscure the image, but it makes the image unusable on any site other than the original one. You will see how to place a semitransparent graphic on top of an image in Chapter, ‘‘Manipulating Images and Bitmaps,” and with the help of the information in this chapter, you’ll be able to scan a folder that has thousands of image files and to automate the process of watermarking the images.
Another example has to do with matching filenames to values stored in a database. Product images are usually named after the product’s ID and stored in separate files. There’s a need for programs to match product IDs to images, to find out whether there’s an image for a specific product in the database, or to simply move the image files around (store the images for different product categories into different folders and so on).
To manipulate folders and files, as well as file input/output (I/O) operations, the Framework provides the System.IO namespace. The My object provides My.Computer.FileSystem component, which simplifies the basic file tasks. Obviously, there’s an enormous overlap between the two components.
The FileSystem component is a subset of the IO namespace in terms of the functionality it exposes, but it’s considerably simpler to use. The My object was designed to simplify some of the most common tasks for the VB developer and, as you may recall from Chapter, “Getting Started with Visual Basic 2008,” it’s a speed-dial into the Framework. You can perform all common file I/O operations with a single line of code. (Okay, sometimes you may need a second line, but you get the idea.) To access the full power of the Framework’s I/O capabilities, use the IO namespace. There’s nothing you can do with the My object that you can’t do with the Framework; the opposite isn’t true. The My object was designed to simplify the most common programming tasks, but it’s not a substitute for the Framework.
That said, I will start with a brief overview of the My.Computer.FileSystem component and then I’ll discuss the IO namespace, which is the whole enchilada. Old VB developers will use the My object to access the file system, because VB is about productivity and the My object is simpler. The Framework, on the other hand, is the core of Windows programming and you shouldn’t ignore it.