The My namespace includes several file management features in its My.Computer.FileSystem branch, including features that create streams for reading and writing.
My Namespace Versus Visual Basic Commands
Most of the My.Computer.FileSystem object's members exist to replace or supplement file management features already present in Visual Basic. Table 15-2 lists some of the long-standing file and directory interaction features in Visual Basic, and their equivalents in My.Computer.FileSystem.
Why would Microsoft introduce so many new My features that duplicate existing Visual Basic features? Perhaps it's a way to bring consistency to file-based programming practices through a more object-oriented approach. Or maybe it's yet another move by Microsoft, the United States government, the Knights Templar, Burger King, and other groups set on world domination by controlling you, your family, and your community through the "hidden hand" of extra-long source code statements.
Reading and Writing Files Through My
The My.Computer.FileSystem.OpenTextFileReader and parallel OpenTextFileWriter methods provide shortcuts to the filename-based constructor for StreamReader and StreamWriter objects. This statement:
Dim inputStream As IO.StreamReader = _ My.Computer.FileSystem.OpenTextFileReader( _ fileNamePath)
is identical to:
Dim inputStream As New IO.StreamReader(fileNamePath)
For me, the second version is better due to its terse nature, but it's between you and your source code review team as to which one you will use.
If you want to load the entire contents of a file into either a String or a Byte array, there's no need to open a stream now that My includes the My.Computer.FileSystem.ReadAllText and related ReadAllBytes methods. This statement dumps the entire content of a file into a String.
Dim wholeFile As String = _ My.Computer.FileSystem.ReadAllText( _ fileNamePath)
The My.Computer.FileSystem.WriteAllText and WriteAllBytes methods do the same thing, but in the opposite direction. There's an append Boolean argument that lets you either append or replace the new content relative to any existing content in the file.
My.Computer.FileSystem.WriteAllText( _ fileNamePath, dataToWrite, True) ' True=append
One feature that has always been missing from Visual Basic is the ability to conveniently scan a delimited file (such as tab-delimited or comma-delimited) or a fixed-width-field file, and extract the fields on each line without a lot of extra parsing code. Visual Basic now includes the Microsoft.VisualBasic.FileIO.TextFieldParser object that simplifies this process. This object lets you indicate either a field delimiter (such as the tab character) or an array of column sizes. Once you associate it with a file path, it reads each data line, breaking up the distinct fields for you into a string array. The My.Computer.FileSystem.OpenTextFieldParser method opens the file and defines the parsing method in one fell swoop.
Dim dataFields() As String Dim sourceFile As FileIO.TextFieldParser ' ----- Open the file with tab-delimited fields. sourceFile = My.Computer.FileSystem.OpenTextFieldParser( _ sourceFilePath, vbTab) ' ----- Process each line. Do While Not sourceFile.EndOfData dataFields = sourceFile.ReadFields() ' ----- dataFields is a simple string array, ' so you can examine each field directly. If (dataFields(0) = "NEW") Then ' ----- and so on... Loop sourceFile.Close()
The TextFieldParser can also detect comment lines and ignore them silently. I am sure that it's using a StreamReader secretly hidden inside the object's black box. While the internals are hidden from view, the exposed features of this object make it a snap to process field-based text files.