File Processing in VB.NET - Overview

 

Three basic file types are native to Windows (and MS-DOS before it) and can be processed by application programs. These are:

 

text files                       Text files generally consist of "plain text" that is readable to the human eye; if you were to open such a file in a text editor such as Notepad, you could readily discern its content. In a business data processing context, a text file consists of a series of lines (or "records"), each of which contains a set of fields. A data file of this nature is also known as a "flat file".

 

binary files                   Binary files are files which contain data elements that are stored in native machine format. Examples would be executable files (such as .EXE or .DLL files) and files produced by applications such as Excel and Word (.XLS and .DOC files). This would also include data files (record-oriented files) that store one or more fields (such as an Integer field) in native machine format. If you were to look at such files in a text editor such as Notepad, while you may be able to see some readable blocks of text, for the most part it would look like "gibberish".

 

random files                 Random files are data files that support "direct access" by record number. This means that the application can go directly to the desired record in a file rather than reading through all the preceding records to get to the desired record (as would be required with a text file). Random files were most commonly used prior to the advent of desktop database systems such as MS-Access, but they can still be used today.

 

Early versions of BASIC such as GW-BASIC and QBasic, as well as all versions of "Classic" (pre-.NET) Visual Basic could process these types of files using native file I/O statements. In later versions of Classic VB (5 and 6), the File System Object (FSO) became available. The FSO provided an object-oriented model to perform file system operations as well as methods for reading and writing sequential text files, however the FSO did not have methods for processing binary or random files.

 

VB.NET provides functionality to process text and binary files through the System.IO namespace and also supports re-tooled versions of earlier file processing functions through the Microsoft.VisualBasic namespace. Random files can also be processed, but only via the MS.VB namespace. Although the FSO can still be used in .NET, that would require the use of the COM Interop layer; thus, the use of the functionality provided by the System.IO and MS.VB namespaces is preferable.  

 

Text File Processing

 

To process text files using the System.IO namespace, you use the FileStream object to refer to the file being processed, and the StreamReader object if processing the file for input or the StreamWriter object if processing the file for output. You then use the methods of the appropriate object to process the file. Methods of the StreamReader object include Peek, Read, ReadLine, ReadToEnd, and Close. Methods of the StreamWriter object include Write, WriteLine, and Close.

 

To process text files using the Microsoft.VisualBasic namespace, the FileOpen and FileClose functions are used to open and close the file, respectively. If processing the file for input, the Input, LineInput, InputString, and EOF are among the functions you might use. If processing the file for output, Write, WriteLine, Print, and PrintLine are among the functions you might use.

 

Binary File Processing

 

To process binary files using the System.IO namespace, you use the FileStream object to refer to the file being processed (as is the case when processing text files), and the BinaryReader object if processing the file for input or BinaryWriter object if processing the file for output. You then use the methods of the appropriate object to process the file. Methods of the BinaryReader object include PeekChar, Read, ReadDataType, and Close. (For ReadDataType, DataType is the type of data you expect to read at the current location – so the names of the methods actually are ReadInt32, ReadSingle, ReadString, etc.)  Methods of the BinaryWriter object include Write and Close.

 

To process binary files using the Microsoft.VisualBasic namespace, the FileOpen and FileClose functions are used to open and close the file, respectively. If processing the file for input, you'll use the FileGet function; if processing the file for output, you'll use the FilePut function.

 

Random File Processing

 

To process random files, you must use the Microsoft.VisualBasic namespace, and the same functions that are used process binary files are used to process random files: FileOpen, FileClose, FileGet, and FilePut. However, the arguments to these functions in this case will be appropriate to random file processing.

 

The next several topics will cover the wide range of VB.NET's file processing capabilities.