By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

File Size

P: n/a
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.)

Oct 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Doug wrote:
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
Oct 4 '06 #2

P: n/a
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?

Oct 4 '06 #3

P: n/a
Jay

Doug wrote:
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

Oct 4 '06 #4

P: n/a
I started thinking it might just be rounding up as I was looking into
this more. Thanks!

Oct 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.