Monday, December 20, 2010

Input/Output

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)
Next

// 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")
Console.WriteLine("Folders:")

For Each dirInfo As DirectoryInfo In dir.GetDirectories()
Console.WriteLine(dirInfo.Name)
Next

Console.WriteLine("Files:")

For Each fi As FileInfo In dir.GetFiles()

Console.WriteLine(fi.Name)
Next

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

Console.WriteLine("Folders:");

foreach (DirectoryInfo dirInfo in dir.GetDirectories())

Console.WriteLine(dirInfo.Name);
Console.WriteLine("\nFiles:");

foreach (FileInfo fi in dir.GetFiles())
Console.WriteLine(fi.Name);

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.CreateText("mynewfile.txt")
File.Copy("mynewfile.txt", "newfile2.txt")
File.Move("newfile2.txt", "newfile3.txt")

// C#

File.CreateText("mynewfile.txt");
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")
fi.CreateText()
fi.CopyTo("newfile2.txt")
Dim fi2 As New FileInfo("newfile2.txt")
fi2.MoveTo("newfile3.txt")

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

No comments:

Post a Comment