Working With The File System & Streams (4)
来源:互联网 发布:淘宝内部券领取 编辑:程序博客网 时间:2024/06/10 09:35
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.
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.
- Working With The File System & Streams (4)
- Working With The File System & Streams (1)
- Working With The File System & Streams (2)
- Working With The File System & Streams (3)
- Working with Streams
- Working with Streams 使用流
- Working with Real-Time Media Streams
- 使用流(Working with Streams)
- Working With System Events
- Working with System Permissions
- Working with System Permissions
- Working with System Permissions
- 3: Change File Permissions(Challenge: Working with the Command Line)
- Accessing the Local File System with Flex
- Working with the DOM
- Windows Streams - An Introduction to File System Streams
- Windows Streams - An Introduction to File System Streams
- android-Working with System Permissions
- Working With The File System & Streams (1)
- Working With The File System & Streams (2)
- Working With The File System & Streams (3)
- java Blog系统推荐-roller
- 2006公务员考试-杂七杂八常识(2)
- Working With The File System & Streams (4)
- 24小时第五季的测试
- VS2005开发本地代码Smartphone程序,菜单编辑器有严重Bug
- 我们向刻神这一伟大目标前进吧!
- 公司管理
- 免费的linux系统虚拟实验室
- 发现自己一天居然没说过一句话
- 把刀插在朋友的肋上 -- 王朔
- 不依赖于DLL就能工作的全局钩子