Skip to main content

How to Get the Size of a File with FileInfo.Length in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

FileInfo.Length gives you a file's size in bytes, as a long. Create a FileInfo with the file's path, read Length, and divide by 1024.0 when you want kilobytes.

Microsoft's reference for FileInfo.Length: Gets the size, in bytes, of the current file. FileInfo is a class in the System.IO namespace that describes one file. You create it with the file's path, and its properties tell you about that file, such as its Name, whether it Exists, and its Length. Length is a long, so the variable you keep it in is a long too.

Syntax​

Syntax
FileInfo <file> = new(<path>);
long <size> = <file>.Length;

long <size> = new FileInfo(<path>).Length;
PartWhat it is
FileInfoThe System.IO class that describes one file
new(<path>)Creates a FileInfo for the file at <path>, such as "note.txt". A path with no folder points into the folder the program runs in
<file>.LengthThe file's size in bytes, read from the disk
long <size>Length is a long, so it goes into a long variable
new FileInfo(<path>).LengthThe same in one line, when the size is all you need

The Example​

A console app called FileSize first writes three small files: a note with the text Buy milk, an empty file, and a file of 3,000 letters. Then it reads each file's size with FileInfo.Length, and turns the biggest one into kilobytes.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n FileSize
cd FileSize
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/FileSize/FileSize.csproj:
Determining projects to restore...
Restored /Users/you/source/FileSize/FileSize.csproj (in 44 ms).
Restore succeeded.

dotnet new console -n FileSize creates a folder named FileSize with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or FileSize.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First make the three files to measure:

Program.cs (three files)
// Make three files to measure: a short note, an empty file and 3,000 letters.
File.WriteAllText("note.txt", "Buy milk");
File.WriteAllText("empty.txt", "");
File.WriteAllText("letters.txt", new string('a', 3000));

File.WriteAllText(<path>, <text>) creates the file and writes the text into it. The names have no folder, so the files are created in the folder the app runs in: the project folder, when you start it there with dotnet run. new string('a', 3000) is a string of 3,000 letters a. A new console app already imports the System.IO namespace that File and FileInfo belong to, so the code builds without a using line.

Then read the size of each file:

Program.cs (each file's size)
// FileInfo describes one file. Its Length is the file's size in bytes.
string[] names = ["note.txt", "empty.txt", "letters.txt"];
foreach (string name in names)
{
FileInfo file = new(name);
Console.WriteLine($"{file.Name,-12} {file.Length,5} bytes");
}

new(name) creates a FileInfo for each name. file.Name is the file's name and file.Length its size in bytes. ,-12 and ,5 give the name and the size a fixed width, so the columns line up.

Last, turn a size into kilobytes:

Program.cs (kilobytes)
// Length is a long. Divide it by 1024 for kilobytes.
FileInfo letters = new("letters.txt");
long size = letters.Length;
Console.WriteLine($"letters.txt is {size / 1024.0:0.0} KB");

size is a long, like Length. A kilobyte here is 1,024 bytes: dividing by 1024.0 gives kilobytes with a fraction, and :0.0 prints one decimal place.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
note.txt 8 bytes
empty.txt 0 bytes
letters.txt 3000 bytes
letters.txt is 2.9 KB

macOS-style terminal window titled Terminal — FileSize showing dotnet run printing note.txt 8 bytes, empty.txt 0 bytes, letters.txt 3000 bytes, and letters.txt is 2.9 KB

note.txt holds Buy milk, 8 characters, and its Length was 8 bytes. The empty file was 0 bytes, and the file of 3,000 letters was 3000 bytes, which printed as 2.9 KB. Each of these letters took one byte on disk. That isn't true of every character, as the FAQ shows.

The Complete File​

macOS-style code window titled Program.cs — FileSize showing File.WriteAllText making note.txt, empty.txt and letters.txt, a loop that creates a FileInfo for each name and prints its Name and its Length in bytes, and the Length of letters.txt kept in a long and divided by 1024.0 for kilobytes

Program.cs
// Make three files to measure: a short note, an empty file and 3,000 letters.
File.WriteAllText("note.txt", "Buy milk");
File.WriteAllText("empty.txt", "");
File.WriteAllText("letters.txt", new string('a', 3000));

// FileInfo describes one file. Its Length is the file's size in bytes.
string[] names = ["note.txt", "empty.txt", "letters.txt"];
foreach (string name in names)
{
FileInfo file = new(name);
Console.WriteLine($"{file.Name,-12} {file.Length,5} bytes");
}

// Length is a long. Divide it by 1024 for kilobytes.
FileInfo letters = new("letters.txt");
long size = letters.Length;
Console.WriteLine($"letters.txt is {size / 1024.0:0.0} KB");

Common Mistakes​

MistakeWhat happensDo this instead
Keeping Length in an intint size = file.Length; didn't build: error CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)Keep it in a long, as the example does: long size = letters.Length;
Reading Length of a file that isn't thereFor missing.txt, Exists printed False, and then Length stopped the program: Unhandled exception. System.IO.FileNotFoundException: Could not find file, followed by the file's full pathCheck file.Exists first, and read Length only when it's True
Reading Length again after the file has changedLength printed 3 bytes, and still 3 after File.AppendAllText added 10 more characters. The FileInfo reference: When the properties are first retrieved, FileInfo calls the Refresh method and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.Call Refresh() before you read it again. After log.Refresh(); it printed 13 bytes

FAQ​

Is the size in bytes the same as the number of characters?​

Not always. Length counts bytes, and some characters take more than one. The text €5 has 2 characters, but the file holding it was 4 bytes:

Program.cs (bytes and characters)
// Two characters, but the euro sign takes more than one byte.
string price = "€5";
File.WriteAllText("price.txt", price);
FileInfo file = new("price.txt");
Console.WriteLine($"Characters: {price.Length}, bytes on disk: {file.Length}");
Output
Characters: 2, bytes on disk: 4

File.WriteAllText saves text as UTF-8. Its reference: This method uses UTF-8 encoding without a Byte-Order Mark (BOM), so using the GetPreamble method will return an empty byte array. And the UTF8Encoding reference: UTF-8 is a Unicode encoding that represents each code point as a sequence of one to four bytes. So a file can hold more bytes than characters.

Can I get the size of a folder with FileInfo.Length?​

No. FileInfo is for files. For a folder named photos, Exists printed False, and Length threw the same FileNotFoundException as for a missing file. The Length reference lists this case among the reasons for that exception: The Length property is called for a directory.

Conclusion​

FileInfo.Length is a file's size in bytes, as a long. Create a FileInfo with the file's path, read Length, and divide by 1024.0 for kilobytes. Check Exists before you read it, call Refresh() when the file may have changed since you last read it, and remember that it counts bytes, not characters.

Additional Resources​