.NET 3.5+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.
Directory Class
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.
C:\test\
d1
f3.txt
f4.txt
d2
d3
f7.txt
f8.txt
f5.txt
f6.txt
f1.txt
f2.txt
Obtaining Drives
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();
/* RESULTS
C:\
D:\
*/
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");
/* RESULTS
c:\test\f1.txt
c:\test\f2.txt
*/
Filtering Files
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.*");
/* RESULTS
c:\test\f1.txt
*/
Searching Subfolders
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);
/* RESULTS
c:\test\f1.txt
c:\test\f2.txt
c:\test\d1\f3.txt
c:\test\d1\f4.txt
c:\test\d2\f5.txt
c:\test\d2\f6.txt
c:\test\d2\d3\f7.txt
c:\test\d2\d3\f8.txt
*/
25 June 2011