views:

107

answers:

5

Are they equavalent? Are they alternative? Is one of them depricated one other? Which one is working better in a ASP.NET web app. sepcially when I was to extract all files of a specific driectory recursively?

Many Thanks in Advance

A: 

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.

Meiscooldude
+1  A: 

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.

Josh Einstein
A: 

DirectoryInfo has a DirectoryInfo.GetFiles Method that probably meet your requirements.

Jakob Gade
+2  A: 

Directory class is a static class which can be used to create, move, enumerate directories and sub directories. The DirectoryInfo class is also served for the same purpose like Directory class where its members are instance members as opposed to Directory 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, the DirectoryInfo class is associated with a folder and provides you all the operations that can be done on the folder. The DirectoryInfo 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.

Incognito
That 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 Einstein
A: 

Directory

  1. Directory is a static class
  2. This should be used when we want to perform one operation in the folder
  3. As There is not any requirement to create object for directory class , so not any overhead for using this

Directory Info Class

  1. Directory Info is not any static class 2.If user is required to perform lot of operation on one directory like creation, Deletion, file listing etc, then directory infor class should be used
  2. A seprate object is created for performing all directory related operations. 4.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
Chuckie