This post will discuss how to get the size of a file in C#.

The System.IO namespace provides several classes pertaining to various common operations on files, directories, and streams. The FileInfo class provides several utility methods and properties for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.

The recommended approach to retrieve the size of a file is using the FileInfo.Length property. It returns the size of the current file, in bytes. If the specified file does not exist, the FileNotFoundException is thrown. This is demonstrated below:

Download Code

 
The following example displays the name and size of the specified file, using the Name and Length properties, respectively:

Download Code

That’s all about getting the size of a file in C#.