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

Programmers Heaven: C# School

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

lbx.Items.Add("Common Application:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

lbx.Items.Add("Local Application Data:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));

lbx.Items.Add("Cookies:

\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Cookies));

}

When I executed the program with the above changes on my system, I got the following result:

Manipulating Files using System.IO.File and System.IO.FileInfo classes

We can manipulate files and perform different operations on them using the System.IO.File and System.IO.FileInfo classes. The System.IO.File class exposes static methods to perform various operations on files.

On the other hand, the object of type System.IO.FileInfo class represents a single file through which we can get/set different properties of a file. Let us practice them one by one:

System.IO.File class

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

Member

Description

Copy()

Copies the specified file to the specified target path.

Create()

Creates the specified file.

Delete()

Deletes the specified file.

292

 

Programmers Heaven: C# School

Exists()

GetAttributes()

GetCreationTime()

GetLastAccessTime()

GetLastWriteTime()

Move()

Open()

OpenRead()

OpenWrite()

SetAttributes()

Returns Boolean value indicating whether the specified file exists.

Returns an object of type System.IO.FileAttributes which contain different information regarding file like whether its is hidden or not.

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

Returns an object of type DateTime that represents the date time of the last access to this file.

Returns an object of type DateTime that represents the date time of the last write action to this file.

Moves the specified file to the specified path.

Opens the specified file and returns the System.IO.FileStream object for this file.

Opens the specified file for reading purpose and returns a read only System.IO.FileStream object for this file.

Opens the specified file for reading purpose and returns a read/write System.IO.FileStream object for this file.

Accepts an object of type System.IO.FileAttributes which contain different information regarding file and set these attributes to the file.

Most of the above methods are very straight forward and it is difficult to show them working in a sample application and its output. So we will consider some of them individually to demonstrate how we can use them in our applications.

Creating a file using Create() method

Suppose we wish to create a file named “c-sharp.txt” on the root folder of C drive. We can write the following statement to do this:

File.Create("C:\\c-sharp.txt");

Author’s Note: To compile the program containing the above and following statements in this section, you need to add the

System.IO namespace in the source file of your program like

using System.IO;

293

Programmers Heaven: C# School

Copying and Moving a file using Copy() and Move() methods

Now if we want to copy this file to C:\my programs folder, we can use the following statement:

File.Copy("C:\\c-sharp.txt", "c:\\my programs\\c-sharp.txt");

Similarly you can use the Move() method to move a file. Also you can use the overloaded form of the Copy() and Create() methods that take a Boolean value to indicate whether you wish to overwrite this file if the file with the same name exists in the target path. E.g.,

File.Copy("C:\\c-sharp.txt", "c:\\my programs\\c-sharp.txt", true);

Checking the existence of the file using Exists() method

This method can be used to check the existence of the file

if(!File.Exists("C:\\c-sharp.txt"))

{

File.Create("C:\\c-sharp.txt");

}

Getting Attributes of a file using GetAttributes() method

We can check the attributes of a file using the GetAttributes() method

FileAttributes attrs = File.GetAttributes("c:\\c-sharp.txt");

lbx.Items.Add("File 'c:\\c-sharp.txt'");

lbx.Items.Add(attrs.ToString());

When I executed the program, I got the information that this is an archive file. Similarly you can set the attributes of the file by using the FileAttributes enumeration

294

Programmers Heaven: C# School

System.IO.FileInfo class

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

Member

Description

CreationTime

Gets or sets the time of creation of this file.

Directory

Returns a DirectoryInfo object that represents the parent directory (folder) of this file.

DirectoryName

Returns the name of the parent directory (in string) of this file.

Exists

Returns Boolean value indicating whether the specified file exists.

Extension

Returns the extension (type) of this file (e.g., .exe, .cs, .aspx).

FullName

Returns the full path and name of the file (e.g., C:\C-Sharp.txt).

LastAccessTime

Returns an object of type DateTime that represents the date time of the last access to this

 

file.

LastWriteTime

Returns an object of type DateTime that represents the date time of the last write action to

 

this file

Length

Returns the size (number of bytes) in a file.

Name

Returns the name of the file (e.g., C-Sharp.txt).

CopyTo()

Copies this file to the specified target path.

Create()

Creates this file.

Delete()

Deletes this file.

MoveTo()

Moves this file.

Open()

Opens this file with various read/write and sharing privileges.

OpenRead()

Opens this file for reading purpose and returns a read only System.IO.FileStream object

 

for this file.

OpenWrite()

Opens this file for reading purpose and returns a read/write System.IO.FileStream object

 

for this file.

OpenText()

Opens this file and returns a System.IO.StreamReader object with UTF8 encoding that

 

reads from an existing text file.

A quick and simple example

Although almost all the above properties and methods are understandable just by reading their name; we still need to create a simple example to demonstrate the functionality of the FileInfo class. In the following example, we will simply perform different operations on a file and display the result in a list box named ‘lbx’

Author’s Friendly Note: I think I have made the worst use of my time in learning programming languages when I read something, thought that ‘It is so easy, I have understood it to 100% and I don’t need to implement a program for this’. Remember most humans just can’t learn even Console.WriteLine() without actually writing it in the IDE, compiling and executing the program.

295

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