A Look at C# Directory and DirectoryInfo Classes - Using the Directory Class
(Page 3 of 4 )
As we have said, the Directory Class contains only static members and it's best used when you need to perform one or two operations on a specific directory. The idea is that there is no need to instantiate a DirectoryInfo class just to perform one operation on a directory; instead, use the static methods of the Directory Class to perform this operation. The following example uses the Directory Class to list its subdirectories.
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
string[] subDirs = Directory.GetDirectories(@"F:MyFolder");
foreach(string subDir in subDirs)
{
Console.WriteLine("Folder Full Name: " + subDir.ToString());
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
Copy and Paste the code in place of the Class1.cs code and press F5 to run the application. You will get the following screenshot:

Because we just need to print out the name of the subdirectories of the directory named MyFolder we have used the Directory.GetDirectories() static method. This method accepts a path to a directory and returns its subdirectories as a string array. Bear in mind that the DirectoryInfo.GetDirectories() method returns an array of DirectoryInfo objects that we can manipulate (as we have done in the previous section). But the Directory.GetDirectories() method returns the subdirectories as an array of strings, and contains only the full name of the subdirectories.
You should also note that the Directory.GetDirectories() accepts the name of the folder that you need to return its subdirectories, whereas the DirectoryInfo.GetDirectories() doesn't accept the folder path because it's working on a specific instance of a directory. Now let's modify the example to return DirectoryInfo objects for the subdirectories of the MyFolder directory.
Passing the Path to Instantiate DirectoryInfo
In the following code we create a DirectoryInfo object by passing the string that returns from the Directory.GetDirectories() method. Let's take a look at the code:
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
string[] subDirs = Directory.GetDirectories(@"F:MyFolder");
foreach(string subDir in subDirs)
{
DirectoryInfo subDInfo = new DirectoryInfo(subDir);
Console.WriteLine("Folder Full Name: {0}", subDInfo.FullName);
Console.WriteLine("Creation Date : {0}", subDInfo.CreationTime);
Console.WriteLine("Attributes: {0}", subDInfo.Attributes);
Console.WriteLine();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
When you run the above code you will get the following screenshot

The only trick in this example is that we have used the string array that returns from calling the Directory.GetDirectories() method to construct DirectoryInfo objects inside the foreach statement. The code simply prints out some of the properties of the DirectoryInfo objects like FullName, CreationTime and Attributes. The Directory Class has methods like GetCreationTime(), GetLastAccessTime(), and GetParent() that correspond to some of the properties of the DirectoryInfo class, but of course there are members that are unique to each class. For example, the Directory.GetCurrentDirectory() returns the directory of the application.
Next: Deleting Directories >>
More C# Articles
More By Michael Youssef