AddThis Social Bookmark Button

Print

C# Input/Output Classes Simplified
Pages: 1, 2, 3, 4, 5

Delete

Deletes an existing directory and, optionally, all of its subdirectories and files. If the directory does not exist, a DirectoryNotFoundException will be thrown. This method has two overloads, as follows:




public static void Delete(string path);
public static void Delete(string path, bool recursive); 

The first overload allows you to pass an empty directory to delete. If the directory is not empty, an IOException will be thrown. The second overload enables you to delete a directory and all of its subdirectories and files, if the recursive argument is set to true.

Exists

Returns true if the specified directory exists; otherwise, it returns false. Its single argument is the path to the directory.

GetDirectories

Returns an array of strings containing all of the subdirectories of a directory. The first overload is the most straightforward; it accepts a valid path to the directory, the subdirectories of which you want to obtain. The second overload allows you to specify a pattern as the second argument. For example, with the second overload, you can obtain only subdirectories that have names beginning with a certain substring, as illustrated by the following code, which prints all of the subdirectories of the C:\ drive that start with the letter W or w:


string[] dirs = Directory.GetDirectories("C:\\", "W*");
int count = dirs.Length;
for (int i=0; i<count; i++)
  System.Console.WriteLine(dirs[i]);

GetFiles

Returns all of the files in a directory. Like the GetDirectories method, this method has two overloads. The first overload returns an array of Strings containing the filenames in a directory. The second overload allows you to specify a pattern as the second argument. For example, the following code prints all of the files in the C:\ drive that start with the letter A or a:


string[] files = Directory.GetFiles("C:\\", "A*");
int count = files.Length;
for (int i=0; i<count; i++)
  System.Console.WriteLine(files[i]);

GetLogicalDrives

Returns an array of Strings containing all of the logical drive names of the current computer in the format <drive_letter>:\. For example, the following code prints all of the drive names in the computer:


string[] drives = Directory.GetLogicalDrives();
int count = drives.Length;
for (int i=0; i<count; i++)
  System.Console.WriteLine(drives[i]);

GetParent

Returns a DirectoryInfo object representing the parent directory of the specified directory. Its single argument is the path of the directory, the parent of which is to be returned.

Move

Moves a specified directory and all of its subdirectories and files to a new location. This method accepts two arguments: the path of the source directory, and the path to the new location.

GetCurrentDirectory

Returns a String object containing the full path to the current directory. Ths method has no parameters.

SetCurrentDirectory

Sets a new current directory. This method's only argument is the path of the directory to make current.

The DirectoryInfo Class

The Directory class provides a convenient way of manipulating directories. The convenience comes at a price, however: the Directory class performs security checks on all of its methods. If you are going to access the same directory multiple times, you can avoid these security checks by using the DirectoryInfo class, which provides methods similar to those in the Directory class, but with all of its methods instance methods. Therefore, you must first construct a DirectoryInfo object by passing a path to its constructor, as in the following code:


DirectoryInfo dirInfo = new DirectoryInfo("C:/data");

Constructing a DirectoryInfo object does not mean creating a directory if it does not yet exist. To create a directory, you need to call the Create method, as shown in the following code:


DirectoryInfo dirInfo = new DirectoryInfo("
     C:/XFiles.txt");
dirInfo.Create();

If the directory already exists, the call to the Create method does not throw an exception. If, after constructing a DirectoryInfo object representing a non-existent directory, however, you call other methods that require that the directory exist, a DirectoryNotFoundException will be thrown. For example, the Delete, GetDirectories, GetFile, and MoveTo methods require the DirectoryInfo object to represent a directory that exists.

Pages: 1, 2, 3, 4, 5

Next Pagearrow