MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 5: Directory

2009-04-16 21:26:35 | 206 views | csharp directory path security

The Basics



The Directory class represents a directory. Its constructor is private, so you cannot use the new keyword to construct a Directory object. That is not necessary, in any case, because all methods of the Directory class are static. The Directory class performs security checks on all of its methods, resulting in slightly reduced performance.


Getting into it



The following are some of the important methods of the Directory class:


CreateDirectory


Creates a directory in the specified path. If the directory's parent does not exist, the parent directory will also be created. For example, if you pass the path C:\data\projects\RD to this method and the projects directory does not exist, both the RD and projects directories will be created. The return value of this method is a DirectoryInfo object representing the created directory. For example:



DirectoryInfo dirInfo = Directory.CreateDirectory("
C:/data/Projects/RD");
System.Console.WriteLine(dirInfo.Name); // prints "RD"



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[italic]);



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[italic]);



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[italic]);



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.


Source: http://www.ondotnet.com/pub/a/dotnet/2002/08/05/io.html?page=2


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.