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. 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值