Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Using the StreamReader Class and My.Computer.FileSystem to Open Text Files
In addition to the Visual Basic commands that open and display text files, there are two additional techniques that you can use to open text files in a Visual Studio program: the StreamReader class and the My object. Because these techniques use .NET Framework objects that are available in all Visual Studio programming languages, I prefer them over the “Visual Basic only” functions. However, Microsoft has been careful to preserve multiple file operation mechanisms for aesthetic and compatibility reasons, so the choice is ultimately up to you.
The StreamReader Class
The StreamReader class in the .NET Framework library allows you to open and display text files in your programs. I'll use this technique several times in this book when I work with text files (for example, in Chapter 16, “Inheriting Forms and Creating Base Classes”). To use the StreamReader class, you add the following Imports statement to the top of your code, which provides access to the StreamReader class:
Imports System.IO
Then, if your program contains a text box object, you can display a text file inside the text box by using the following program code. (The text file opened in this example is Badbills.txt, and the code assumes an object named TextBox1 has been created on your form.)
Dim StreamToDisplay As StreamReader StreamToDisplay = New StreamReader("C:\vb05sbs\chap13\text browser\badbills.txt") TextBox1.Text = StreamToDisplay.ReadToEnd StreamToDisplay.Close() TextBox1.Select(0, 0)
StreamReader is a .NET Framework alternative to opening a text file by using the Visual Basic FileOpen function. In this StreamReader example, I declare a variable named StreamToDisplay of the type StreamReader to hold the contents of the text file, and then I specify a valid path for the file I want to open. Next I read the contents of the text file into the StreamToDisplay variable by using the ReadToEnd method, which retrieves all the text in the file from the current location (the beginning of the text file) to the end of the text file and assigns it to the Text property of the text box object. The final statements close the text file and use the Select method to remove the selection in the text box.
The My Object
The second alternative to opening text files in a program is a new feature of Visual Studio 2005 that uses the My object. The My object is a “speed dial” or rapid access feature designed to simplify accessing the .NET Framework to perform common tasks, such as manipulating forms, exploring the host computer and its file system, displaying information about the current application or its user, and accessing Web services. Most of these capabilities were previously available through the .NET Framework Base Class Library, but due to its complexity, many programmers found the features difficult to locate and use.
The My object is organized into several categories of functionality, as shown in the following table.
Object | Description |
My.Application | Information related to the current application, including the title, directory, and version number. |
My.Computer | Information about the hardware, software, and files located on the current (local) computer. My.Computer includes My.Computer.FileSystem, which you can use to open text files and encoded files on the system. |
My.Forms | Information about the forms in your current Visual Studio project. Chapter 16 shows how to use My.Forms to switch back and forth between forms at run time. |
My.User | Information about the current user active on My.Computer. |
My.WebServices | Information about Web services active on My.Computer, and a mechanism to access new Web services. |
The My Object is truly a “speed dial” feature, fully explorable via the Microsoft IntelliSense feature of the Code Editor. For example, to use a message box to display the name of the current computer followed by the name of the current user in a program, you can simply type:
MsgBox(My.User.Name)
This produces output similar to the following:
The My.Computer object can display many categories of information about your computer and its files. For example, the following statement displays the current system time (the local date and time) maintained by the computer:
MsgBox(My.Computer.Clock.LocalTime)
You can use the My.Computer.FileSystem object along with the ReadAllText method to open a text file and display its contents within a text box object. Here's the syntax you can use if you have a text box object on your form named txtNote (as in the last sample program) and you plan to use an open file dialog object named OpenFileDialog1 to get the name of the text file from the user:
Dim AllText As String = "" OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT" OpenFileDialog1.ShowDialog() 'display Open dialog box If OpenFileDialog1.FileName <> "" Then AllText = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName) txtNote.Text = AllText 'display file End If
The ReadAllText method copies the entire contents of the given text file to a string variable or object (in this case, a string variable named AllText), so in terms of performance and coding time, ReadAllText is faster than reading the file one line at a time with the LineInput function.
Because of this speed factor, the My object provides an excellent shortcut to many common programming tasks and is one of the brighter features of Visual Basic 2005. It is important to take note of this new feature and its possible uses, but the My object is useful here because we are reading the entire text file. The LineInput function and StreamReader class offer more features than the current implementation of the My object, and especially the ability to process files one line at a time (a crucial capability for sorting and parsing tasks, as we shall soon see). So it is best to master each of the three methods for opening text files discussed in this chapter. The one you use in actual programming practice will depend on the task at hand, and the way you plan to use your code in the future.