Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory vs DirectoryInfo

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.

like image 333
Afshar Mohebi Avatar asked Jun 30 '10 05:06

Afshar Mohebi


People also ask

What is difference between directory and DirectoryInfo class in c#?

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.

What is the difference between the directory class and the DirectoryInfo class in the system IO namespace?

The DirectoryInfo class shares almost all of the same properties as the FileInfo class, except that they operate on directories not files. The DirectoryInfo class does not have static methods and can only be used on instantiated objects. The DirectoryInfo object represents a physical directory on a disk.

What is DirectoryInfo in C#?

DirectoryInfo class is a part of System.IO namespace. It is used to create, delete and move directory. It provides methods to perform operations related to directory and subdirectory. It is a sealed class so, we cannot inherit it.

What is a directory class?

DirectoryInfo Class (System.IO)Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.


2 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.

like image 180
Josh Avatar answered Sep 28 '22 08:09

Josh


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.

like image 43
Incognito Avatar answered Sep 28 '22 08:09

Incognito