469,946 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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 1716
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

Post your reply

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

Similar topics

7 posts views Thread by Steven Burn | last post: by
1 post views Thread by Phil Price | last post: by
2 posts views Thread by Pat | last post: by
6 posts views Thread by coriolis_wong | last post: by
2 posts views Thread by moishyyehuda | last post: by
3 posts views Thread by Gary Wessle | last post: by
5 posts views Thread by googlenewsgroups | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.