416

I need a way to get the size of a file using C#, and not the size on disk. How is this possible?

Currently I have this loop

foreach (FileInfo file in downloadedMessageInfo.GetFiles())
{
    //file.Length (will this work)
}

Will this return the size or the size on disk?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JL.
  • 78,954
  • 126
  • 311
  • 459

7 Answers7

570

If you have already a file path as input, this is the code you need:

long length = new System.IO.FileInfo(path).Length;
live-love
  • 48,840
  • 22
  • 240
  • 204
  • 6
    Surround it with try/catch block and check all possible exceptions as described [here](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.-ctor): SecurityException, UnauthorizedAccessException, PathTooLongException, NotSupportedException and - dependend on your use case - ArgumentNullException and ArgumentException – huha Mar 09 '20 at 14:00
426

FileInfo.Length will return the length of file, in bytes (not size on disk), so this is what you are looking for, I think.

Luca Cremonesi
  • 144
  • 2
  • 3
  • 13
Marcin Deptuła
  • 11,789
  • 2
  • 33
  • 41
  • 6
    Also: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/85bf76ac-a254-41d4-a3d7-e7803c8d9bc3 this is a discussion about how to get "the other value", which is size on disk. – Marcin Deptuła Sep 04 '09 at 18:39
  • 25
    There is no method "FileInfo.Length"! Remember that you specified call of STATIC METHOD. Real answer is `new FileInfo(path).Length` what can be heavy for mass operation. – Vincent Jan 14 '21 at 14:48
  • 5
    FWIW @Vincent - Yes and No. [`FileInfo.Length`](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.length?view=net-5.0) is how the doc refers to the property, even though it is not static. The answer is relative to the code in question. Consider how one typically does a "mass operation" - via the directory info: `... new DirectoryInfo(...).GetFiles();` A collection of `FileInfo`s is created for you. Getting `Length` of all those files is lightweight. – ToolmakerSteve Jul 30 '21 at 11:04
  • 1
    @ToolmakerSteve: MS docs is well-known rubbish, don't refer it as a "normal way". Newbies will be confused when they see call to static method and obviously it doesn't exist. GetFiles() is only one of ways to get list of files. Names could be autogenerated (like "pic15.jpg", "pic16.jpg"...) - then you need to call FileInfo every time. In any case OS keeps cache of directories and getting length MUST NOT be heavy operation as "create object" - length can be obtained directly by call like FileInfo.GetLength(string fileName). It's WAY more convenient and suits to many casual tasks. – Vincent Jul 31 '21 at 14:11
  • 2
    @Viincent - *"Names could be autogenerated (like "pic15.jpg", "pic16.jpg"...) - then you need to call FileInfo every time. "* That's almost never necessary, for a "mass operation". Extract the directory path of all the file paths, into a `HashSet`. **I'd love to know about a static method that gives Length of file. What environment are you in, that has `FileInfo.GetLength`?** Neither Google nor Intellisense is giving me a hit for any **static** member, on either `File` or `FileInfo`, that gives file length. – ToolmakerSteve Jul 31 '21 at 16:17
44

FileInfo.Length will do the trick (per MSDN it "[g]ets the size, in bytes, of the current file.") There is a nice page on MSDN on common I/O tasks.

jason
  • 236,483
  • 35
  • 423
  • 525
13

MSDN FileInfo.Length says that it is "the size of the current file in bytes."

My typical Google search for something like this is: msdn FileInfo

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
10

The FileInfo class' Length property returns the size of the file (not the size on disk). If you want a formatted file size (i.e. 15 KB) rather than a long byte value you can use CSharpLib, a package I've made that adds more functionality to the FileInfo class. Here's an example:

using CSharpLib;
FileInfo info = new FileInfo("sample.txt");
Console.WriteLine(info.FormatBytes());  // Output: 15 MB
S. Ferrell
  • 171
  • 3
  • 11
5

It returns the file contents length

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
4

Size on disk might be different, if you move the file to another filesystem (FAT16, NTFS, EXT3, etc)

As other answerers have said, this will give you the size in bytes, not the size on disk.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78