473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Size

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
4 3626
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
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
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
I started thinking it might just be rounding up as I was looking into
this more. Thanks!

Oct 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
4
by: M P | last post by:
Can you help me find an asp code that will upload a file from my PC to web server? Mark
11
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: BW | last post by:
I am creating an upload/download function for an extranet site. Files will be uploaded to directory based upon the users login and associated project. The function works as long as I use "c:\Temp"...
2
by: Dan | last post by:
Hi, I know this code is not entirely javascript, but bare with me. Can you please tell me why this does not work: page: filemanager.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
2
by: cleary1981 | last post by:
Hi, I have created a script in PHP thats generates an SVG image. Want I want to do is have PHP save this as example.svg. Can this be done? Heres my code <?php require "config.php"; $proj_id =...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.