Monday, December 20, 2010


Working with the File System
Enumerating Drives:
To list all the drives connected to a computer, use the static DriveInfo.GetDrives
method (in the System.IO namespace) to retrieve a collection of DriveInfo objects.
example, the following loop outputs a list of all drives to the console:

' VB
For Each di As DriveInfo In DriveInfo.GetDrives()
Console.WriteLine(" {0} ({1})", di.Name, di.DriveType)

// C#
foreach (DriveInfo di in DriveInfo.GetDrives())
Console.WriteLine(" {0} ({1})", di.Name, di.DriveType);

DriveInfo has the following properties:
1. AvailableFreeSpace Indicates the amount of available free space on a drive
 2.DriveFormat Gets the name of the file system, such as NTFS or FAT32
3. DriveType Gets the drive type
4. IsReady Indicates whether a drive is ready
5. Name Gets the name of a drive
6. RootDirectory Gets the root directory of a drive
7. TotalFreeSpace Gets the total amount of free space available on a drive
8. TotalSize Gets the total size of storage space on a drive
9. VolumeLabel Gets or sets the volume label of a drive

Managing Files and Folders

The .NET Framework provides classes that you can use to browse files and folders, create
new folders, and manage files

Browsing Folders

You can use the DirectoryInfo class to browse folders and files. First, create an instance
of DirectoryInfo by specifying the folder to browse.
Then, call the DirectoryInfo.Get-Directories or DirectoryInfo.GetFiles method.
The following example displays the files and folders in the C:\Windows\folder.
' VB

Dim dir As New DirectoryInfo("C:\Windows")

For Each dirInfo As DirectoryInfo In dir.GetDirectories()


For Each fi As FileInfo In dir.GetFiles()


// C#
DirectoryInfo dir = new DirectoryInfo(@"C:\Windows");


foreach (DirectoryInfo dirInfo in dir.GetDirectories())


foreach (FileInfo fi in dir.GetFiles())

Creating Folders

To create folders, create an instance of DirectoryInfo and then call the DirectoryInfo
.Create method. You can check the boolean DirectoryInfo.Exists property to determine
if a folder already exists. The following sample checks for the existence of a folder and
creates it if it doesn’t already exist, although the Common Language Runtime (CLR)
does not throw an exception if you attempt to create a folder that already exists.

' VB

File.Copy("mynewfile.txt", "newfile2.txt")
File.Move("newfile2.txt", "newfile3.txt")

// C#

File.Copy("mynewfile.txt", "newfile2.txt");
File.Move("newfile2.txt", "newfile3.txt");

Alternatively, you can create an instance of the FileInfo class representing the file and
call the Create, CreateText, CopyTo, MoveTo, and Delete methods. The following code
performs the same functions as the previous sample:

' VB

Dim fi As New FileInfo("mynewfile.txt")
Dim fi2 As New FileInfo("newfile2.txt")

// C#
FileInfo fi = new FileInfo("mynewfile.txt");
FileInfo fi2 = new FileInfo("newfile2.txt");
To delete a file, create an instance of the FileInfo class and then call FileInfo.Delete.

No comments:

Post a Comment