Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

314 Part II Programming Fundamentals


By using an OpenFileDialog control to prompt the user for the file’s path, you can let the
user choose which text file to open in a program. This control contains the Filter property,
which controls the type of files displayed; the ShowDialog method, which displays the
Open dialog box; and the FileName property, which returns the path specified by the user.
The OpenFileDialog control doesn’t open the file; it just gets the path.
There are several ways to read text files, but the two most common ways are to use the My
namespace or the StreamReader class. The StreamReader class offers more features than
the My namespace, in particular the ability to process files one line at a time (a capability
that might be needed for sorting and parsing tasks). So it is best to master both methods
for opening text files discussed in this chapter. The one that 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.

The My Namespace


The My namespace is a 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 namespace was added in Microsoft Visual Studio
2005 to make programming easier.

The My namespace is organized into several categories of functionality, as shown in
Table 13-1. (My.Log, My.Response, and My.Request are not listed here because they are
designed for ASP .NET applications only .)

TABLE 13-1 The My Namespace
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 14,
“Managing Windows Controls and Forms at Run Time,” shows how to
use My.Forms to switch back and forth between forms at run time.
My.Resources Information about your application’s resources (read only). Allows you to
dynamically retrieve resources for your application.
My.Settings Information about your application’s settings. Allows you to dynamically store
and retrieve property settings and other information for your application.
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.
Free download pdf