How do you get the file size in C#?

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?


Solution 1:

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

long length = new System.IO.FileInfo(path).Length;

Solution 2:

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

Solution 3:

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.