C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It provides methods that interact with the file system. With it we create directories, get file lists from directories, test directories for existence, and even delete directories.
GetFiles. The Directory.GetFiles method returns a string array of file names. It receives the directory name and an optional pattern parameter. This is a useful and popular Directory method.
Tip: This method causes a file system access. If you call it repeatedly, and the results seldom change, consider caching its result.
EnumerateFiles. Many methods on the Directory type are available. We call them as static methods: to call EnumerateFiles, we can use Directory.EnumerateFiles. Next, we see the syntax for EnumerateFiles.
For this method, we receive one path string at a time. EnumerateFiles returns an IEnumerable collection. With a foreach-loop, we can get each string one at a time. If there are many files, this can avoid a large array creation.
C# program that uses EnumerateFiles using System; using System.IO; class Program { static void Main() { // Returns an IEnumerable<string>. foreach (string path in Directory.EnumerateFiles("C:\\")) { Console.WriteLine(path); } } }
Out of curiosity, I tested Directory.EnumerateFiles against GetFiles. The EnumerateFiles loop is used in the foreach-loop statement. But for GetFiles, I allocated an array outside the foreach-loop.
Next: We see that for a directory with 113 files, GetFiles is faster. But for a directory with 1251 files, EnumerateFiles is faster.
Info: The explanation for this is that an array creation of 1251 elements is avoided with EnumerateFiles.
EnumerateFiles code fragment: C# foreach (string path in Directory.EnumerateFiles("C:\\")) { } GetFiles code fragment: C# string[] paths = Directory.GetFiles("C:\\"); foreach (string path in paths) { } Time results: 113 files EnumerateFiles: 463681.00 ns GetFiles: 448693.00 ns (faster) Time results: 1251 files EnumerateFiles: 3128232.00 ns (faster) GetFiles: 3197795.00 ns
Exists. If you want to be certain that a directory exists, use the Directory.Exists method. Please be aware though that a directory could be moved or deleted at any time. So your code must still handle exceptions, even if Exists is first used.
Tip: The Directory.Exists method can be accessed most easily if you add using System.IO to the top of your program.
Tip 2: The method returns a bool: true if the directory is present, and false if it is not.
Next: This program shows that on my computer C:\Users exists, but C:\Losers does not. Please forgive the bad joke.
C# program that uses Directory.Exists using System; using System.IO; class Program { static void Main() { if (Directory.Exists("C:\\Users")) { Console.WriteLine("Users"); } if (Directory.Exists("C:\\Losers")) { Console.WriteLine("Losers"); } } } Output Users
CreateDirectory. The Directory type provides support for creating a new directory. The CreateDirectory method makes new folders. This method throws many different exceptions, so preparing for this is important.
DirectoryInfo. The DirectoryInfo is another way of accessing the Directory type functionality. We access important properties and methods on a DirectoryInfo. We create DirectoryInfo by passing a directory path to its constructor. It is a class.
Next: This program gets the name of the directory from DirectoryInfo, and then gets the file count for the directory.
C# program that uses DirectoryInfo using System; using System.IO; class Program { static void Main() { // Get info. DirectoryInfo info = new DirectoryInfo(@"C:\deves\"); // Write name. Console.WriteLine(info.Name); // Write file count. FileInfo[] array = info.GetFiles(); Console.WriteLine(array.Length); } } Output deves 5
Size. There are many ways to sum the total size of a directory. With one approach, we call GetFiles and loop through all the file names. We use the FileInfo type on each file name. This is the slower approach.
Another option is to use the DirectoryInfo type. Then call the GetFileSystemInfos method to get all the FileInfos at once in an array. This is much faster. It is sometimes around ten times more efficient.
Exception. One exception that can be thrown when using Directory methods is DirectoryNotFoundException. If you call Directory.GetFiles, and the Directory does not exist, this exception will be provoked.
GetDirectoryName. The Path type provides a GetDirectoryName method. This does not access the file system. Instead, it receives a string argument and returns the directory part of that path string. It is helpful when using directory-handling code.
DriveInfo. A drive is a physical directory on your computer. The C drive is often the main hard drive on a Windows computer. With the DriveInfo class, we can easily handle drives in C# programs.
Windows. There are special methods used to interact with directories on Windows. In Windows Forms, the FolderBrowserDialog provides a window that displays folder hierarchies. This avoids the need for custom code.
Also: The OpenFileDialog allows users to browse directories and open files, not just other folders.
FileSystemWatcher. Usually for best performance, avoiding the Directory type is worthwhile. We can use the FileSystemWatcher type to get notifications when a file changes in a directory. Then, we can update our memory model of the file system.
Tip: This makes it possible to access the file system only when needed. It makes using a cache of file names more effective.
Summary. There are many important methods on the Directory type. For interacting with the file system and using folders, this type is helpful. Other types, such as Path, help with directory-handling code.