473,405 Members | 2,334 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,405 software developers and data experts.

How to format the file size

Hi,

I have made a window application that get file information from the server the file size return in bytes. I need help on how to format the file size accordingly so that it will return the file size in either "Bytes", "K's" or "Meg's".

Below's the script that I have for format the file size. But it doesn't work.
Can you please help me? Thanks a lot.


private void lstFiles_SelectedIndexChanged_1(object sender, EventArgs e)
{

try
{
lblDir.Text = treeView1.SelectedNode.FullPath;
string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
FileInfo fi = new FileInfo(path);

lblFile.Text = lstFiles.SelectedItem.ToString();
lblSize.Text = fi.Length.ToString();
if (fi.Length >= 1024 * 1024 * 1024)
{
string.Format("{0:########0.00} GB", ((double)fi.Length) / (1024 * 1024 * 1024));
}
else if (fi.Length >= 1024 * 1024) string.Format("{0:####0.00} MB", ((double)fi.Length) / (1024 * 1024));
else if (fi.Length >= 1024) string.Format("{0:####0.00} KB", ((double)fi.Length) / 1024);
else string.Format("{0} bytes", fi.Length);
lblCreated.Text = fi.CreationTime.ToString();
}
catch { }
}
Apr 14 '07 #1
5 1922
Motoma
3,237 Expert 2GB
What in the code doesn't work? Does fail completely? Does it generate any error messages? Does it give the wrong numbers? Does it get drunk and prank call it's ex-girlfriends?
Please elaborate, we need to know what we are looking for.
Apr 15 '07 #2
Basically when we get the file information from the server, we will get the file size in bytes. My intention is how to make it returns in bytes, KB or MB accordingly to the the file size. To make this work, i added the code as below in the script to format the file size. But, all the file size still return in bytes and no change to KB, MB or GB. The script gives no error and i still can run the script. It just that the script that i added in as below seem no function. I have no idea whether the coding writen as below to format the file size correctly or not and why it cannot work. I am new in this area, please be patient to me...
Thanks in advance.

if (fi.Length >= 1024 * 1024 * 1024)
{
string.Format("{0:########0.00} GB", ((double)fi.Length) / (1024 * 1024 * 1024));
}
else if (fi.Length >= 1024 * 1024) string.Format("{0:####0.00} MB", ((double)fi.Length) / (1024 * 1024));
else if (fi.Length >= 1024) string.Format("{0:####0.00} KB", ((double)fi.Length) / 1024);
else string.Format("{0} bytes", fi.Length);
Apr 15 '07 #3
Motoma
3,237 Expert 2GB
You are doing string formatting, however, you are never assigning the formatted string to your label. You are doing label assignment before your string formatting.
Apr 15 '07 #4
I got it!!!!!! Very very thanks for your hint!!!
By the way, I have one more question...
How to Sort files in a directory by size? For now, the files show in the list box of my window application are sorted by Name, not size.
Can you please guide me on this?
Thanks a lot.
Apr 15 '07 #5
Motoma
3,237 Expert 2GB
You will have to write a sort routine using one of the standard algorithms.
Apr 16 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Steven Burn | last post by:
Anyone able to tell me if there's an alternative to the Format function that VB has? (it appears ASP doesn't support it) I'm basically (and believe me, I know how stupid this is going to sound)...
1
by: Phil Price | last post by:
Hi there, I'm developing a shape recognition application for the tablet PC for a) fun b) university project. Currently I'm working on the learning stage using neural networks, and have to store...
3
by: marwa mohamed | last post by:
salamo alikom hi all when i run report that includes image control linked to a field in the table that contains the image path(on the server)and the record source of the report contains over 30...
2
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
6
by: coriolis_wong | last post by:
Hi, I need to transfer csv format file to DBase III format file. How do i do it in Python language? Any help is appreciated. Thanks.
14
by: Manish | last post by:
The project I am developing doesn't involves database. I want to parse the mailbox file (.mbx) and store the summary in the text file for fast retrieval and display of information in the Inbox...
2
by: moishyyehuda | last post by:
Hi I am writing a script to upload images. How do I check the format of the file? How can I tell if the file is an image, movie, or text file? I would also like to put a limit on how much a user...
3
by: Gary Wessle | last post by:
Hi I wrote 2 functions "included at the bottom" one inserts and one reads data from a file, the data format is as here **************** a 2 b -0.0336974 _time 1.17312e+09 ****************
5
by: googlenewsgroups | last post by:
I have a custom set of objects that is correctly serializing & deserializing the object tree. However in some cases the size of the resultant serialization is larger than I would expect. Is there...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.