Retrieve Drive, File and Folder Names
Many software projects involve interaction with the file system. When this requires that a list of drive letters is generated, or that a drive or folder's files and subfolders are retrieved, the Directory class can be used to obtain the list.
The Directory class is found in the System.IO namespace. It provides a number of static methods that can be used to obtain information about the drives, files and folders of a system, in addition to other methods that allow the file system to be modified or additional information to be obtained. In this article we will look at several methods that allow you to generate arrays containing drive, file or folder lists, or let you enumerate the same information. The methods that return arrays are available in the .NET framework version 1.1. The others require .NET 3.5.
The example code shows you how to use these methods and provides some results based upon the following folder structure. This structure describes the contents of a test folder, which contains two folders and two text files. The subfolders each contain two further files, with the "d2" folder containing yet another subfolder holding two files.
The first method that we will consider is the simplest, as it has no parameters. GetLogicalDrives returns an array of strings, each representing one of the drives in the file system. Only the drive names are retrieved. If you need further information, such as the drive type, label or size, you should use the DriveInfo class.
The following code obtains a list of drives. The results are those from a system with two disks with the letters C and D. Note that the drive letters are expressed as paths with a trailing backslash character (\).
var drives = Directory.GetLogicalDrives();
Obtaining the Files in a Folder
When you need a list of the files within a folder you can use the GetFiles method. This has three overloaded versions. The first includes a single string parameter that accepts the path of the folder that you wish to interrogate. The paths and names of the files within the folder are returned in the resultant array. This includes hidden and system files but not directory names.
var files = Directory.GetFiles(@"c:\test");
The second overload allows a filename filter to be applied to limit the files that are returned. The filter is provided as the second argument. It can include the asterisk (*) and question mark (?) wildcard characters. An asterisk will be matched to any group of characters. A question mark matches any single character. The following code retrieves all of the files in the c:\test folder that have names starting with "f1.":
var files = Directory.GetFiles(@"c:\test", "f1.*");
The final overload allows you to indicate whether you wish to search subfolders of the target folder. The overload has three parameters. The first two arguments match those of the previous example, allowing you to specify the target folder and a filter string. The third parameter accepts a value from the SearchOption enumeration. The value may be either AllDirectories, indicating that subfolders will be searched, or TopDirectoryOnly, if you wish to exclude subfolders.
The following sample code retrieves all of the files in the c:\test folder and its subfolders:
var files = Directory.GetFiles(@"c:\test", "*.*", SearchOption.AllDirectories);
25 June 2011