C# GetFileSystemInfos Method

Programming tip

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

This C# article shows how the GetFileSystemInfos method can be used.

Example benchmark

Note

To get the total size of a directory, it is necessary to get a FileInfo on every file in the directory. The method Size1 below uses the GetFileSystemInfos method and then casts each element of the result array to FileInfo, which has a Length property. It computes the sum of these lengths. On the other hand, the Size2 method gets an array of all the files in the directory and then creates a FileInfo on each one individually.

Program that uses GetFileSystemInfos [C#]

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
Performance optimization

Result. 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 Directory.GetFiles, and GetFileSystemInfos may have a faster implementation for many files.

Notes

Note

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. Other optimizations could also be 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

The C# programming language

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. This approach can be used to get information about a directory's files much faster than is possible by using a string array.

File Handling
.NET