A Look at C# Directory and DirectoryInfo Classes
(Page 1 of 4 )
Have you ever tried to understand the organization and interaction between the classes in the System.IO namespace? In this article I talk about two of the classes available in this namespace, the Directory and DirectoryInfo classes. You will learn how to manipulate directories. You will also learn how to use the properties of those classes to return some useful information.
Note that we will discuss the classes that manipulate the file system objects, files (discussed in the next article) and directories (folders). The .NET Framework provides us with other classes for reading and writing to files, which will be discussed in a separate article. To get the most out of this article you need to have a good knowledge of C# basics, and of course you need to have VS.NET 2003 installed on your machine.
I will use the F drive on my hard disk for the examples for this article, but you are free to use whatever drive you want. You should use a drive that doesn't contain important files because you may unintentionally delete all the folders and the files on your drive (if you like to play with the code, like me), so be careful. Let's begin our discussion with the Directory and the DirectoryInfo classes.
The Directory and the DirectoryInfo Classes
The Directory and the DirectoryInfo classes are used to represent a folder in the file system. But there are critical differences between the two. The Directory class has only static methods, and it is used when you need to perform just one operation on a folder. Since you don't have to create an object to represent the directory to perform one operation, all of the methods of the Directory class are static, so you perform the operation without creating an instance of that class.
If you need to perform a lot of operations on a folder, it's better to create an object that represents the specific folder. The class DirectoryInfo is used to create an object to represent a folder. It's effective if you are going to perform many operations on the folder because, once the object is created, it has all the necessary information about the folder such as its creation time, last access time and attributes. All the members of the DirectoryInfo class are instance members, unlike the Directory class which contains only static members.
Let's look at an example that uses the DirectoryInfo class. Create a new VS.NET Console Application and name it IOProject, then copy the following code and paste it in the file Class1.cs. Don't forget to replace the F drive with your drive.
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please enter the name of the folder you want to create or use");
// store the folder name in the dirName variable
string dirName = Console.ReadLine();
// create the DirectoryInfo instance
DirectoryInfo dInfo = new DirectoryInfo(@"F:" + dirName);
// check if the directory doesn't exist, if so then create it
if(!dInfo.Exists)
{
dInfo.Create();
}
Console.WriteLine("How many subfolders you want to create?");
// store the number of subfolders in the variable
// subDirs
int subDirs = Convert.ToInt32(Console.ReadLine());
// create the subfolders using a for statement
for(int i = 1; i <= subDirs; i++)
{
dInfo.CreateSubdirectory(dirName + i);
}
Console.WriteLine("The following Subfolders have been" + " created in the folder " + dirName);
// get the subfolders using the
// DirectoryInfo.GetDirectories() method
DirectoryInfo[] dirs = dInfo.GetDirectories();
// printing the subfolders full name
foreach(DirectoryInfo subDir in dirs)
{
Console.WriteLine(subDir.FullName);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
Run the application and enter the values that are shown in the next screenshot.

So the folder name is MyFolder and we need to create four subfolders. After you run the application, navigate to MyFolder to take a look at the four new folders.

Let's walk through the code step-by-step. The second statement in the Main method uses the Console.ReadLine() method to get the folder name from the user and stores it in the variable dirName. Next we create a DirectoryInfo object by passing the folder name (stored in the dirName variable) concatenated with the drive letter, so it's much more like "F:MyFolder." Note that instantiating the DirectoryInfo object doesn't create the folder in the File System, so we next use the property DirectoryInfo.Exists to see whether the folder already exists in the File System or not.
If the folder doesn't exist (the property returns false) we create it using the method DirectoryInfo.Create(). If it does exists (the property returns true) we will start manipulating the folder, because we have the folder in the file system and we have created the object that can manipulate it. We have used the operator ! to reverse the Boolean value of the Exists property, so if the directory doesn't exist it returns false and the ! operator reverses the value, causing the if statement to evaluate to true and execute its body.
We use the variable subDirs to store the number of subfolders that will be created in our folder by assigning the return value of the Console.ReadLine() method and converting it to the int data type using the Convert.ToInt32() method. The next for statement creates the subfolders using the CreateSubDirectory() method of the DirectoryInfo instance, which is passed the name of the folder as a string. Note that the method CreateSubDirectory() will do nothing if the subfolder already exists.
So far, so good. We then use the dInfo.GetDirectories() method to return an array of type DirectoryInfo that represents the subfolders of the current folder. Next, we use a foreach statement to iterate through the individual DirectoryInfo objects to write its full name (using the property FullName) to the console using the DirectoryInfo.FullName property. In the next section, we will modify the example a little bit.
Next: Modifying the Example >>
More C# Articles
More By Michael Youssef