tags:

views:

821

answers:

5

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 size or size on disk?

+6  A: 

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

Ravadre
Thanks just checking because you just never know...
JL
@JL: However, the documentation does. You could also just do a simple test.
Jason
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.
Ravadre
+2  A: 

It returns the file contents length

Thomas Levesque
+2  A: 

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
A: 

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
A: 

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