Are they equivalent or alternatives to each other? Is any of them deprecated and if so, which one? Which one is recommended for use in an ASP.NET web application? My aim is to extract all files from a specific directory recursively.
5 Answers
Directory is a static class that provides static methods for working with directories. DirectoryInfo is an instance of a class that provides information about a specific directory. So for example, if you wanted the information about C:\Temp:
var dirInfo = new DirectoryInfo("C:\\Temp");
if (dirInfo.Exists) {
FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
...
}
If you just wanted the names as strings, it might be quicker and easier to avoid creating an instance of DirectoryInfo by using the static methods of Directory.
if (Directory.Exists("C:\\Temp")) {
string[] files = Directory.GetFiles("C:\\Temp", "*.*", SearchOption.AllDirectories);
...
}
In short, it really doesn't matter which you use as long as it does what you want. Neither is recommended over the other.
-
4in addition to the above I would add though that: "If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class, because a security check will not always be necessary." as per the MSDN documentation. – BenKoshy Feb 03 '16 at 03:15
Directory
class is a static class which can be used to create, move, enumerate directories and sub directories. TheDirectoryInfo
class is also served for the same purpose likeDirectory
class where its members are instance members as opposed toDirectory
class. The main difference between the two lies in when we can use these classes.Directory
class can be used when we want to a simple folder operation at once. For example, you need to delete the folder and get away. But, theDirectoryInfo
class is associated with a folder and provides you all the operations that can be done on the folder. TheDirectoryInfo
class accepts a path as parameter when instantiating and provides you everything on the folder. You can create subdirectories, move, enumerate etc. CODEDIGEST
Also an important note if you have to do several actions on directory DirectoryInfo will have performance advantage as it will not require security privileges check on each action.

- 16,567
- 9
- 52
- 74
-
1That may be true from a code-access security perspective if you are running in partial trust but that doesn't apply to ACL security checks which as far as I know are still performed on every operation. – Josh Jun 30 '10 at 05:12
Directory
- Directory is a static class.
- This should be used when we want to perform one operation in the folder.
- As There is not any requirement to create object for Directory class, so not any overhead for using this.
Directory Info Class
- DirectoryInfo is not a static class.
- If user is required to perform lot of operations on one directory like creation, deletion, file listing etc, then DirectoryInfo class should be used.
- A separate object is created for performing all directory related operations.
- 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.

- 2,648
- 3
- 25
- 44

- 125
- 2
DirectoryInfo is (basically) the Directory class but is used in a non-static context. If you are going to be making many calls to the FileSystem, especially when its the same folder or in subdirectory of said folder, MSDN suggests using DirectoryInfo.

- 3,671
- 5
- 27
- 30
DirectoryInfo has a DirectoryInfo.GetFiles method that probably meet your requirements.

- 9,564
- 146
- 81
- 122

- 12,319
- 15
- 70
- 118