Got something to write about? Check out our Article Builder.
*/
*/

VB.NET School - Lesson 15 - Working With The File System & Streams

Next Page



Working With The File System & Streams

If you are new to the VB.NET School
This is the 15th in the series of lessons of our VB.NET School. The VB.NET School is a kind of interactive learning platform where those who want to learn .NET with VB.NET can find help and support. With one issue a week, describing some areas of the VB.NET Programming Language with the Microsoft .NET Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (farazrasheed@acm.org) or the WEBMASTER (info@programmersheaven.com).

For previous lessons click here

Lesson Plan
In this lesson we will learn how we can manipulate the Windows file system and different types of streams using VB.NET and .NET. We will start out by looking at how we can manipulate physical drives, folders and files, and perform different operations on them. In the second half of the lesson, we will explore the different types of streams used in .NET and see how we can read data from and write data to files. Finally we will learn about the concept of serialization of objects and how we can implement serialization in VB.NET. The lesson also contains a supplementary topic on Asynchronous I/O.

Working with the File System
This section is further divided into three parts. In the first part, we will see how we can get the software system's environment information, e.g. the path to the Windows System folder & the Program Files folder, current user name, operating system version, etc. In the second part, we will learn how to perform common file operations such as copying, moving, deleting, renaming and more. In the last part of this section, we will explore directory (or Folder) manipulation techniques.

Obtaining the Application's Environment Information - The System.Environment class
The System.Environment class is the base class used to obtain information about the environment of our application. The description of some of its properties and methods is presented in the following table:

MemberDescription
CurrentDirectoryGets or sets the directory name from which the process was started
MachineNameGets the name of computer on which the process is currently executing
OSVersionReturns the version of current Operating System
UserNameReturns the user name of the user executing this process
VersionReturns a System.Version object representing the complete version information of the common language runtime (CLR)
Exit()Terminates the current process, returning the exit code to the Operating System
GetFolderPath()Returns the complete path of various standard folders of the windows operating system like Program files, My documents, Start Menu and etc.
GetLogicalDrives() Returns an array of type string containing the list of all drives present in the current system.


Demonstration Application - Environment Information
Let's make a demonstration application that uses the above mentioned properties and methods to display environment information. It is a windows application project that contains a list box named 'lbx' which is used to display the information, a button named 'btnGo' which will be used to start fetching the information and a button named 'btnExit' which terminates the application. The main logic is present inside the Go button's event handler:

    Private Sub btnGo_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnGo.Click
        Dim os As OperatingSystem = Environment.OSVersion
        Dim OSid As PlatformID = os.Platform
        Dim drives() As String = Environment.GetLogicalDrives()
        Dim drivesString As String = "" 
        Dim drive As String
        For Each drive In drives
            drivesString += drive + ", "
        Next
        drivesString = drivesString.TrimEnd(" "c, ","c)
        lbx.Items.Add("Machine Name:
            " + Environment.MachineName)
        lbx.Items.Add("Operating System: " + Environment.OSVersion.ToString())
        lbx.Items.Add("Operating System ID:  " + OSid.ToString())
        lbx.Items.Add("Current Folder:          " + Environment.CurrentDirectory)
        lbx.Items.Add("CLR Version:             " + Environment.Version.ToString())
        lbx.Items.Add("Present Drives:          " + drivesString)
    End Sub
Here we have simply retrieved the environment information from the public properties and methods and added them to the list box. A few important things to note here are:

  • Environment.GetLogicalDrives() returns an array of strings, with each string representing the drive name.
  • Environment.Version returns an object of type System.Version which contains the detailed information about the current version of the common language runtime (CLR).
The event handler for the exit button simply calls the Exit() method of the Environment class to terminate the current process.

    Private Sub btnExit_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnExit.Click
        Environment.Exit(0)
    End Sub
Here we have passed zero to the Exit() method. This value will be returned to the Operating System and can be used to check whether the program terminates successfully or not.

When I executed this program to my system, I got the following result:
http://www.programmersheaven.com/articles/faraz/lesson15_img1.gif



School Home
Next Page







corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings