Directory vs DirectoryInfo

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)