DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 11.10 Creating, Moving, and Deleting a Directory

Problem

You need to create a new directory, move an existing directory, or delete a directory.

Solution

The System.IO namespace contains two classes to perform these actions: the Directory and DirectoryInfo classes. The Directory class contains only static methods, while the DirectoryInfo class contains only instance methods.

To create a directory, you can use the static CreateDirectory method of the Directory class. The return value for this method is an instance of the DirectoryInfo class. This class can be used to invoke instance methods on the newly created directory. For example:

DirectoryInfo dirInfo = null;
if (!Directory.Exists(@"c:\delete\test"))
{
    dirInfo = Directory.CreateDirectory(@"c:\delete\test");
}

You can also use the instance Create method of the DirectoryInfo class—a method that takes no parameters and returns void. For example:

DirectoryInfo dirInfo = null;
if (!Directory.Exists(@"c:\delete\test"))
{
    dirInfo = new DirectoryInfo(@"c:\delete\test");
    dirInfo.Create( );
}

To move a directory, you can use the static Move method of the Directory class, which returns void. For example:

if (!Directory.Exists(@"c:\MovedDir"))
{
    Directory.Move(@"c:\delete", @"c:\MovedDir");
}

You can also use the instance MoveTo method of the DirectoryInfo class, which returns void. For example:

DirectoryInfo dirInfo = null;
if (!Directory.Exists(@"c:\MovedDir"))
{
     dirInfo = new DirectoryInfo(@"c:\delete\test");
     dirInfo.MoveTo(@"c:\MovedDir");
}

To delete a directory, you can use the static Delete method of the Directory class, which returns void. There are two overloads for this method: one that will attempt to delete just the directory and one that you can pass a Boolean value to tell it to delete recursively. If you elect to delete the directory recursively, all subdirectories and files will be deleted as well. If you do not use the recursive flag, the Delete method will throw an exception if you attempt to delete a directory that has either files or subdirectories still in it:

if (Directory.Exists(@"c:\MovedDir"))
{
    Directory.Delete(@"c:\MovedDir", true);
}

You can also use the instance Delete method of the DirectoryInfo class, which returns a void. For example:

DirectoryInfo dirInfo = null;
if (Directory.Exists(@"c:\MovedDir"))
{
     dirInfo = new DirectoryInfo(@"c:\delete\test");
     dirInfo.Delete(true);
}

Discussion

Creating, moving, and deleting are the basic operations that you can perform on directories. It makes sense that there are specific methods to address each of these operations. In fact, there are two methods to perform each of these actions: one static and one instance method.

Which method you choose depends on what you are trying to accomplish. If you need a quick way of creating, moving, or deleting a directory, use the static methods since you don't incur the overhead of instantiating an object before performing the operation. If you will be performing multiple operations on a directory, you should use instance methods. Another consideration is that static methods on a class do not require an object to be created on the managed heap. Instance methods require an object to be created before the methods can be called. If you are trying to minimize the number of objects the garbage collector has to manage, consider using static methods.

Before creating a new directory, you should first determine whether that directory already exists. The Directory class contains a static method, Exists, to perform this operation (note that there are no instance classes to do this).

To move a directory, you must first determine whether the destination directory exists. If it does exist, the move operation will fail and throw an exception.

To delete a directory, you must first determine whether it exists. If it does not exist, the delete operation will fail and throw an exception.

See Also

See the "Directory Class" and "DirectoryInfo Class" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section