Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

14. The File System & Streams

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

288

Programmers Heaven: C# School

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:

289

Programmers Heaven: C# School

Obtaining the paths of various Windows Standard folders – Environment.GetFolderPath()

The method Environment.GetFolderPath() can be used to get the complete paths of various windows standard folders on the current machine. The only argument passed to the method is a value from the System.Environment.SpecialFolder enumeration. Some of the more common members of this enumeration are:

Member

Description

ProgramFiles

The program files folder where programs are usually installed.

CommonProgramFiles

The Common Files folder of Program Files.

DesktopDirectory

The folder representing the desktop of user.

Favorites

The Favorites folder to store favorite links.

History

The History folder to store history files.

Personal

The My Documents folder.

Programs

The folder representing the Programs menu of Start Menu.

Recent

The Recent folder.

SendTo

The Send To folder.

StartMenu

The Start menu folder.

Startup

Folder of the Startup menu on the Start >> Programs menu.

System

The System folder of Windows folder.

ApplicationData

The Application Data folder.

CommonApplicationData

The Common Application Data folder

LocalApplicationData

The Local Application Data folder

Cookies

The folder used to store cookies setting

Let’s use these in a simple program to understand their functionality. We can modify the previous program to include these results by changing the btnGo_Click method to:

private void btnGo_Click(object sender, System.EventArgs e)

{

290

Programmers Heaven: C# School

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);

lbx.Items.Add("Program Files:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));

lbx.Items.Add("Common Program Files:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));

lbx.Items.Add("Windows Desktop:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

lbx.Items.Add("Favorites:

\t"

+

Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

lbx.Items.Add("History:

\t"

+

Environment.GetFolderPath(Environment.SpecialFolder.History));

lbx.Items.Add("Personal (My Documents:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Personal));

lbx.Items.Add("Start Menu's Program:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Programs));

lbx.Items.Add("Recent:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Recent));

lbx.Items.Add("Send To:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.SendTo));

lbx.Items.Add("Start Menu:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));

lbx.Items.Add("Startup:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Startup));

lbx.Items.Add("Windows System:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.System));

lbx.Items.Add("Application Data:

\t" +

291

Соседние файлы в папке books