Working With The File System & Streams (1)

Working With The File System & Streams

If you are new to C# School
This is the final lesson of our C# School. The C# School is a kind of
interactive learning platform where those who want to learn .NET with C#
can find help and support. With one issue a week, describing some areas of
the C# 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 (
[email protected])or the WEBMASTER ( [email protected]).

For previous lessons:

Introduction to the .Net Framework & C#
Visual Studio.Net and Hello World Console Application
C# Language Fundamentals
Classes and Objects
Inheritance & Polymorphism in C#
Structure, Enumeration, Garbage Collection and Nested Classes
Abstract classes & Interfaces
Arrays, collections and string Manipulation
Exception handling in C#
Delegates and Events
WinForms and Windows Applications
More Windows Controls & Standard Dialog Boxes
Data Access In .NET Using ADO.Net
Multithreading in C#
Lesson Plan
Today we will learn how we can manipulate the Windows file system and
different types of streams using C# 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 C#.
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:

Member Description
CurrentDirectory Gets or sets the directory name from which the process was
started
MachineName Gets the name of computer on which the process is currently
executing
OSVersion Returns the version of current Operating System
UserName Returns the user name of the user executing this process
Version Returns 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 void btnGo_Click(object sender, System.EventArgs e)
{
    OperatingSystem os = Environment.OSVersion;
    PlatformID OSid = os.Platform;

    string[] drives = Environment.GetLogicalDrives();
    string drivesString = "";
    foreach(string drive in drives)
    {
           drivesString += drive + ", ";
    }
    drivesString = drivesString.TrimEnd(' ', ',');

    lbx.Items.Add("Machine Name:       /t" + Environment.MachineName);
    lbx.Items.Add("Operating System:   /t" + Environment.OSVersion);
    lbx.Items.Add("Operating System ID:/t" + OSid);
    lbx.Items.Add("Current Folder:     /t" + Environment.CurrentDirectory);
    lbx.Items.Add("CLR Version:        /t" + Environment.Version);
    lbx.Items.Add("Present Drives:     /t" + drivesString);
}
Here we have simply retrieved the environment information from 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 exit button simply calls the Exit() method of the
Environment class to terminate the current process.


private void btnExit_Click(object sender, System.EventArgs e)
{
  Environment.Exit(0);
}
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: 
 
發佈了563 篇原創文章 · 獲贊 1 · 訪問量 62萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章