Working With The File System & Streams (3)

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
Exists() Returns Boolean value indicating whether the specified file exists
GetAttributes() Returns an object of type System.IO.FileAttributes which
contain different information regarding file like whether its is hidden or
not
GetCreationTime() Returns an object of type DateTime that represents the
date time of the creation of this file
GetLastAccessTime() Returns an object of type DateTime that represents the
date time of the last access to this file
GetLastWriteTime() Returns an object of type DateTime that represents the
date time of the last write action to this file
Move() Moves the specified file to the specified path.
Open() Opens the specified file and returns the System.IO.FileStream object
for this file
OpenRead() Opens the specified file for reading purpose and returns a read
only System.IO.FileStream object for this file
OpenWrite() Opens the specified file for reading purpose and returns a
read/write System.IO.FileStream object for this file
SetAttributes() 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;
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

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
Extention Returns the extention (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.

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:
[PIC]
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.

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. 

 
發佈了563 篇原創文章 · 獲贊 1 · 訪問量 62萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章