Home
C#
Directory Type
This page was last reviewed on Jun 14, 2023.
Dot Net Perls
Directory, System.IO. The Directory type is found in System.IO. It provides methods that interact with the file system's concept of folders.
File
With this class, we create directories, get file lists from directories, test directories for existence, and delete directories. Directory is often used with Path.
Directory.GetFiles
Path
EnumerateFiles. Many methods on the Directory type are available. We call them as static methods: to call EnumerateFiles, we can use Directory.EnumerateFiles.
static
Note For this method, we receive one path string at a time. EnumerateFiles returns an IEnumerable collection.
And 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.
IEnumerable
foreach
using System; using System.IO; class Program { static void Main() { // Returns an IEnumerable<string>. foreach (string path in Directory.EnumerateFiles("C:\\")) { Console.WriteLine(path); } } }
Exists. If you want to be certain that a directory exists, use the Directory.Exists method. A directory could be moved or deleted at any time. So code must still handle exceptions.
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.
return bool
true, false
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"); } } }
Users
DirectoryInfo. The DirectoryInfo is another way of accessing the Directory type functionality. We access important properties and methods on a DirectoryInfo.
Tip We create DirectoryInfo by passing a directory path to its constructor. DirectoryInfo is a class.
Next This program gets the name of the directory from DirectoryInfo, and then gets the file count for the directory.
using System; using System.IO; class Program { static void Main() { // Get info. DirectoryInfo info = new DirectoryInfo(@"C:\perls\"); // Write name. Console.WriteLine(info.Name); // Write file count. FileInfo[] array = info.GetFiles(); Console.WriteLine(array.Length); } }
perls 5
Benchmark, EnumerateFiles. I tested the Directory.EnumerateFiles method against GetFiles(). I tested a directory with just 31 files.
Array
Version 1 This version of the code uses EnumerateFiles to loop over all the file names in the directory. It does not create an array.
Version 2 This version uses GetFiles, and it creates a local array of string file names.
Benchmark
Result It is usually acceptable to use GetFiles. The temporary array does not tend to cause many problems.
using System; using System.Diagnostics; using System.IO; class Program { const int _max = 1000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: use EnumerateFiles. for (int i = 0; i < _max; i++) { foreach (string path in Directory.EnumerateFiles("C:\\")) { } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use GetFiles. for (int i = 0; i < _max; i++) { string[] paths = Directory.GetFiles("C:\\"); foreach (string path in paths) { } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
69516.30 ns EnumerateFiles 69568.30 ns GetFiles
CreateDirectory. The Directory class helpfully provides support for creating a new directory. The CreateDirectory method makes new folders.
Directory.CreateDirectory
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.
Directory Size
Exception. One exception that can be thrown is DirectoryNotFoundException. If you call Directory.GetFiles, and the Directory does not exist, this exception will be provoked.
IOException
GetDirectoryName. The Path type provides a GetDirectoryName method. This receives a string argument and returns the directory part of that path string.
Path.GetDirectoryName
DriveInfo. A drive is a physical directory on your computer. The C drive is often the main hard drive on a Windows computer. DriveInfo helps us handle drives.
DriveInfo
FileSystemWatcher. 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.
FileSystemWatcher
Tip This makes it possible to access the file system only when needed. It makes using a cache of file names more effective.
A summary. For interacting with the file system and using folders, the Directory type is helpful. Other types, such as Path, help with directory-handling code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 14, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.