Determining File Size In Code

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi,

It looks like the only way to get a size of a file within dot net is to
use FileInfo and the Length property. However that only returns the
number of bytes in the file which is translating properly (I have a
file that has a size of 1 KB but has 14 bytes in it so the conversion
isn't working right). Is there some method/property out there that
will get the actual size of the file? Also, would there be a method
like this that will get this size of a file that is being written to?
(I need to write data to a file but need to track how big the file is
getting as I'm writing it.)
 
Is there some method/property out there that
will get the actual size of the file?

You are getting the actual size of the file. The number in Windows Explorer
is just rounded up. I tried this myself, and at 1024 bytes, the file size
reads 1KB in Explorer. At 1025, it's 2. So you need to do your conversion
based on the same type of rounding.

As for reading it from a file that's being written, I think this will work
if you call the Flush() method of the StreamWriter and then do your size
check.
 
The rounding up was kind of what I was coming around to as well as I
was posting this. Thanks for the suggestions, I will give them a try!
 
Back
Top