File Size

D

Doug

Hi,

It looks like the only way to get a size of a file within csharp 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.)
 
C

Carl Daniel [VC++ MVP]

Doug said:
Hi,

It looks like the only way to get a size of a file within csharp 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.)

The size in FileInfo.Length IS the size of the file according to the OS -
there is no other size. I'm quite sure I don't understand what you mean by
"I have a file that has a size of 1KB but has 14 bytes in it" - what reports
the length as 1KB, and what reports that it has 14 bytes in it?

For a file that you're writing, you'll get the most accurate results if you
keep track of the size yourself. If you're writing the file sequentially
through a single FileStream, you can use the Position property of the
FileStream to tell you the current size of the file. Examining the file "on
disk" via FileInfo will likely not report the correct size due to buffering
in the FileStream and elsewhere - the OS file size isn't guaranteed to be
correct until you've at least flushed the stream you're writing to. There
may be circumstances where you have to close the file before the correct
size will be reported.

Also, note that file sizes are always specified in bytes, not characters.
If you're writing a file using a variable-length multi-byte character
encoding (e.g. BIG5 Chineese), then the size of the file in bytes will
differ from the size in characters. There's no way to determine how many
characters are in a file written in a multi-byte encoding other than reading
the entire file, counting complete characters, since there's no way to know
a-priori how many bytes each character occupies.

-cd
 
D

Doug

When I look at a file I created inside Windows Explorer, Windows
Explorer shows the file to be 1 KB. However when I use the FileInfo
Length property on it, I get a value back of 14. 14 is a true value of
the number of bytes I have in the file, but since Windows Explorer is
showing me 1 KB, I am not sure of the best approach for getting the
information I need?
 
J

Jay

Doug said:
When I look at a file I created inside Windows Explorer, Windows
Explorer shows the file to be 1 KB. However when I use the FileInfo
Length property on it, I get a value back of 14. 14 is a true value of
the number of bytes I have in the file, but since Windows Explorer is
showing me 1 KB, I am not sure of the best approach for getting the
information I need?

The 1KB in Windows Explorer is rounding up - if you want to see the
actual file size in Explorer, right click the file and select
Properties from the menu that appears. The actual file size (and the
size on disk) will be displayed in the General tab.

Otherwise I'm not sure what you're after.

-Jay
 
D

Doug

I started thinking it might just be rounding up as I was looking into
this more. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top