C#:.NET:Directory

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

GetFileSystemInfos can get file sizes quickly. With the .NET Framework, you can do things in many ways. For example, you can use different methods from System.IO to get the total size of a directory on the disk.


Example. To get the total size of a directory, it is necessary to get a FileInfo on every file. The method Size1 uses GetFileSystemInfos and then casts each element to FileInfo, which has a Length property. It gets the sum of these lengths.

As: Cast Examples

However: The Size2 method gets an array of all the files in the directory and then creates a FileInfo on each one.

String Array
C# program that uses GetFileSystemInfos

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static int Size1(string dir)
    {
	// Get infos.
	DirectoryInfo directoryInfo = new DirectoryInfo(dir);
	FileSystemInfo[] array = directoryInfo.GetFileSystemInfos();

	// Loop over elements.
	int sum = 0;
	for (int i = 0; i < array.Length; i++)
	{
	    FileInfo fileInfo = array[i] as FileInfo;
	    if (fileInfo != null)
	    {
		sum += (int)fileInfo.Length;
	    }
	}
	return sum;
    }

    static int Size2(string dir)
    {
	string[] array = Directory.GetFiles(dir);
	int sum = 0;
	for (int i = 0; i < array.Length; i++)
	{
	    sum += (int)(new FileInfo(array[i]).Length);
	}
	return sum;
    }

    static void Main()
    {
	Console.WriteLine(Size1("C:\\stage"));
	Console.WriteLine(Size2("C:\\stage"));

	var s1 = Stopwatch.StartNew();
	Size1("C:\\stage");
	s1.Stop();

	var s2 = Stopwatch.StartNew();
	Size2("C:\\stage");
	s2.Stop();

	Console.WriteLine((s1.Elapsed.TotalMilliseconds).ToString("0.00 ms"));
	Console.WriteLine((s2.Elapsed.TotalMilliseconds).ToString("0.00 ms"));
	Console.Read();
    }
}

Output

11376146
11376146
9.99 ms
71.09 ms

The version of the method that called GetFileSystemInfos was several times faster. For the 10.8 megabyte directory here, with 2032 files, it was over 60 milliseconds faster. This is likely because it avoids the call to GetFiles.


Discussion. It's hard to develop faster file handling methods such as this one because the implementation of the methods is not apparent. Conceptually, however, GetFileSystemInfos could be faster because no string array of file names is needed.

Also, other optimizations are possible in the .NET Framework's internal code. This implementation of a directory size algorithm is typically better than the previous one on this site.

Get Directory Size

Summary. We looked at the GetFileSystemInfos method in the C# language on the DirectoryInfo type. Further, we found that casting a FileSystemInfo instance to a FileInfo instance is possible.

Review: This approach can be used to get information about a directory's files much faster than is possible by using a string array.