Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Recursive Directory Searches in C# (Part 1)
Recursive Directory Searches in C# (Part 1)
By Rick Leinecker

Rate This Article: Add This Article To:

Recursive Directory Searches in C# (Part 1)
( Page 1 of 3 )

In Part 1 of this new series, Rick Leinecker shows you how to search a device for a specified file type.

Your boss tells you that management has arrived at a solution that will minimize the impact on the business. Company computers will be scanned at night, and any computer that exceeds a preset amount of space on the hard drive with audio (.mp3) files will be flagged and the department head will deal with it.

He looks at you and waits for a response. You know he wants you to tell him how easy it will be. But you wince as you realize that you don't really know what it will take to get the job done.

This article will show you how to easily accomplish this task—and many more similar tasks. It relies on two .NET classes: the Directory class, and to a much lesser extent, the File class. And to create a more robust version, this article shows how to spawn a thread that calls back to a delegate method. Lastly, we'll need to talk about authenticating to remote machines in case you want to run the process from your machine instead of touching the individual hardware.

ADVERTISEMENT

If this sounds complicated, relax because it's not. Hold on and learn how easy it can be.

The Directory and File classes

The first thing that any rational software developer would do when faced with this task is examine the System.IO namespace. This will show you the classes that allow hard drive (or other device) access. The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

As you scan the System.IO classes, you see that the answer is right in front of you. The Directory class gives a list of directories, and can also provide a list of files. This class gives us the obvious methodology to examine the files on the hard drive.

Make sure that you add a using statement so that System.IO can be referenced as follows:

using System.IO;

Example 1:

The Directory.GetDirectories method from the Directory class returns a list of directories. There are two ways that I prefer to deal with the list of directories. One is as a list of strings, and the other is a series of strings. The following two examples show both of these techniques.

Method 1:

// Create an array of strings
// from the root directory of C:
string[] Dirs = Directory.GetDirectories(@"C:\");

The entire code for displaying the directories from the root of the C drive is as follows:

string[] Dirs = Directory.GetDirectories(@"C:\");
for (int i = 0; i < Dirs.Length; i++)
{
     Console.WriteLine(Dirs[i]);
}

Note that the first argument when calling the Directory.GetDirectories method is the directory that you want to perform the search on such as @"C:\Windows\System32" or @"C:\".

Method 2:

You could also shorten the previous code and make it more C#-like as follows:

foreach (string strDirName in Directory.GetDirectories(@"C:\"))
{
     Console.WriteLine(strDirName);
}

The C# syntax that you choose will be determined by what you decide is the best way to use the list of directories. I have used both of these coding approaches. My decision is always based on what is the shortest and most efficient way to carry out the task.

Please note that the Directory.GetDirectories method returns a list of directories whether you specify the root of the hard drive (C:\) or a subdirectory (such as C:\Windows\System32). It is important to note that subdirectories always start the list with special subdirectories named "." And "..". We will ignore these two entries in the directory list later in this article as we traverse through the directory structure of the hard drive.

Example 2:

The Directory.GetFiles method of the Directory class returns a list of files. As with directories, there are two ways that I prefer to deal with the list of files. One is as a list of strings, and the other is a series of strings. The following two examples show both of these techniques.

Method 1:

// Create an array of strings 
// from the root directory of C:
string[] Files = Directory.GetFiles(@"C:\");

The entire code for displaying the files from the root of the C drive is as follows:

string[] Files = 
     Directory.GetFiles( @"C:\\", "*.* ");
for (int i = 0; i < Files.Length; i++)
{
     Console.WriteLine(Files[i]);
}

Note that as with the Directory.GetDirectories method, the first argument is the directory that you want to perform the search on. The second argument is the file mask that you want to search for. All search for all files has a search mask of "*.*" while an audio search will have a search mask of "*.mp3".

Method 2:

You could also shorten the previous code and make it more C#-like as follows:

foreach (string strFileName 
in Directory.GetFiles(@"C:\", "*,*"))
{
     Console.WriteLine(strFileName);
}

Once again, the C# syntax that you choose will be determined by what you decide is the best way to use the list of files.



 
 
>>> More Languages Articles          >>> More By Rick Leinecker
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.