Working With The File System & Streams (4)

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.

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)
    {
        lbx.Items.Add("/t" + directory);
    }
}
And when I executed the above program on my system, I got the following
result:

[PIC]

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
Delete() Deletes the directory with the specified name
GetDirectories() Returns an array of type DirectoryInfo that represents all
the sub-directories of this directory.
GetFiles() Returns an array of type FileInfo that represents all the files
contained in this directory.
GetFileSystemInfos() Returns an array of type FileSystemInfo that
represents all the files and folders contained in this directory.
MoveTo() Moves this directory and all its contents (files and directories)
to the specified path.
Refresh() Refreshes this instance of DirectoryInfo. 

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