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

Programmers Heaven: C# School

We have written the following code on the ‘Go’ button’s event handler:

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

{

FileInfo file = new

FileInfo("c:\\c-sharp.txt");

lbx.Items.Add("File

Name:

" + file.Name);

lbx.Items.Add("File

Extention:

" + file.Extension);

lbx.Items.Add("File's Full Name:

" + file.FullName);

lbx.Items.Add("Parent Directory:

" + file.DirectoryName);

lbx.Items.Add("File

Size:

" + file.Length.ToString() + " bytes");

lbx.Items.Add("File

Attributes:

" + file.Attributes.ToString());

}

Here we have simply used the properties of the FileInfo class to retrieve and print some information about a file. When I executed this program on my system, I got the following output:

Note: Before executing this program, I changed the attributes of file ‘C:\C-Sharp.txt’ to Readonly, Hidden and Archive using Windows Explorer.

Manipulating Directories (folders) using System.IO.Directory and System.IO.DirectoryInfo classes

Similar to the File and FileInfo classes we can perform several operations on directories using the Directory and DirectoryInfo classes. Again it is worth-noting that the System.IO.Directory class contains static methods while the System.IO.DirectoryInfo class contains instance members to perform various tasks on directories.

296

Programmers Heaven: C# School

System.IO.Directory class

A review of static methods of the Directory class is presented in the following table:

Member

Description

CreateDirectory()

Creates the specified directory.

Delete()

Deletes the specified directory.

Exists()

Returns Boolean value indicating whether the specified directory exists.

GetCreationTime()

Returns an object of type DateTime that represents the date time of the creation of the

 

specified directory.

GetDirectories()

Returns an array of strings containing the names of all the sub-directories of the

 

specified directory.

GetFiles()

Returns an array of strings containing the names of all the files contained in the specified

 

directory.

GetFileSystemEntries()

Returns an array of strings containing the names of all the files and directories contained

 

in the specified directory.

GetParent()

Returns an object of type DirectoryInfo representing the parent directory of the specified

 

directory.

Move()

Moves the specified directory and all its contents (files and directories) to the specified

 

path.

Creating, deleting and checking for the existence of directories

Some code that demonstrates how to perform the above operations is shown below.

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

 

{

 

 

lbx.Items.Add("Directory 'C:\\Faraz' exists:

" +

Directory.Exists("C:\\Faraz"));

lbx.Items.Add("Creating Directory 'C:\\Faraz':

" +

Directory.CreateDirectory("C:\\Faraz"));

lbx.Items.Add("Directory 'C:\\Faraz' exists:

" + Directory.Exists("C:\\Faraz"));

lbx.Items.Add("Parent Directory of 'Faraz' is:

" + Directory.GetParent("C:\\Faraz"));

lbx.Items.Add("Deleting Directory 'C:\\Faraz'...

");

 

Directory.Delete("C:\\Faraz", true);

 

 

lbx.Items.Add("Directory 'C:\\Faraz' exists:

" + Directory.Exists("C:\\Faraz"));

}

 

 

 

 

 

Again, the code simply calls the various static methods of the Directory class to perform these operations. One thing to note here is that we have passed the path-string and a true value to the Directory.Delete() method. Passing the true value as the second parameter tells the runtime environment (CLR) to remove the directory recursively i.e. not only deletes the files in this directory but also delete the files and directories contained in its sub-directories and so on.

297

Programmers Heaven: C# School

Getting the contents (files and sub-directories) of a directory

The Directory class exposes three methods to retrieve the contents of a directory. Directory.GetDirectories() returns a list of all the sub-directories of the specified directory, Directory.GetFiles() returns a list of all the files in the specified directory and Directory.GetFileSystemEntries() returns a list of all the files and sub-directories contained in the specified directory. Let’s get a list of the contents of the Windows folder of your system.

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

{

// get the path of Windows Folder's System Folder

string winFolder = Environment.GetFolderPath(Environment.SpecialFolder.System); // Separate the Windows Folder

winFolder = winFolder.Substring(0, winFolder.LastIndexOf('\\'));

string[] fileSystemEntries = Directory.GetFileSystemEntries(winFolder); string[] files = Directory.GetFiles(winFolder);

string[] directories = Directory.GetDirectories(winFolder);

// show windows folder path

lbx.Items.Add("Address of Windows Folder: " + winFolder);

//show files/folder in windows folder lbx.Items.Add("");

lbx.Items.Add("File System Entries (files/folder) in the Windows Folder: "); foreach(string fileSystemEntry in fileSystemEntries)

{

lbx.Items.Add("\t" + fileSystemEntry);

}

//show files in windows folder

lbx.Items.Add("");

lbx.Items.Add("Files in the Windows Folder: "); foreach(string file in files)

{

lbx.Items.Add("\t" + file);

}

// show folder in windows folder lbx.Items.Add("");

lbx.Items.Add("Directories in the Windows Folder: "); foreach(string directory in directories)

298

Programmers Heaven: C# School

{

lbx.Items.Add("\t" + directory);

}

}

And when I executed the above program on my system, I got the following result:

System.IO.DirectoryInfo class

The System.IO.DirectoryInfo class is also used to perform different operations on directories. Unlike the Directory class, we need to create an object of the DirectoryInfo class to use its services. A review of some of the important methods and properties of the DirectoryInfo class is presented in the following table:

Member

Description

Exists

Returns a Boolean value indicating whether the specified directory exists.

Extention

Returns the extention (type) of this directory.

FullName

Returns the full path and name of the directory (e.g., C:\Faraz).

Name

Returns the name of the directory (e.g., Faraz).

Parent

Returns the full path and name of the parent directory of this directory.

Create()

Creates a directory with the specified name.

299

 

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