wally_davis
asked on
Need help on how to use a routine I was given in C# to format file sizes to KB or MB.
In the code below, I'm trying to take a number like 11284781 to something like 11284KB in the line of code --> "lvsi.Text = Convert.ToString(fi.Length );".
I'm just not sure how to pass whats in the line above to the code in the "private string formatsizekb(double dsize)" Routine.
If anyone has any ideas on how to add "," commas, that would be great but not in dire need of it.
Many thanks,
Wally
I'm just not sure how to pass whats in the line above to the code in the "private string formatsizekb(double dsize)" Routine.
If anyone has any ideas on how to add "," commas, that would be great but not in dire need of it.
Many thanks,
Wally
foreach (System.IO.FileInfo fi in files)
{
lvi = new ListViewItem();
lvi.Text = fi.Name;
lvi.Tag = fi.FullName;
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = Convert.ToString(fi.Length);
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
this.lvwFilesAndFolders.Items.Add(lvi);
}
--------------------------------------------------------------
private string formatsizekb(double dsize)
{
const int iKB = 1024;
const long lMB = 1048576;
if (dsize < iKB)
return string.Format("{0} bytes", dsize);
if (dsize >= iKB && dsize < lMB)
return string.Format("{0} KB", dsize / 1024);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, I'm getting two errors when I attempt to run this.
I get a squiggly line underneath the name "formatsiezekb" in the routine "private string formatsizekb(long dsize)". The errors are:
1. No overload for method 'formatsizekb' takes '0' arguments. (Sounds like I haven't passed anything in)
2. 'ParseLogErrors.Form1.form atsizekb(l ong)': not all code paths return a value.
My application, at this point, uses a ListView control, points to a single folder location and then pulls out and displays the filenames, there size and the last time they were accessed.
I get a squiggly line underneath the name "formatsiezekb" in the routine "private string formatsizekb(long dsize)". The errors are:
1. No overload for method 'formatsizekb' takes '0' arguments. (Sounds like I haven't passed anything in)
2. 'ParseLogErrors.Form1.form
My application, at this point, uses a ListView control, points to a single folder location and then pulls out and displays the filenames, there size and the last time they were accessed.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured it out.
I didn't have an else in my function:
private string formatsizekb(long dsize)
{
const int iKB = 1024;
const long lMB = 1048576;
if (dsize < iKB)
return string.Format("{0} bytes", dsize);
ELSE IF (dsize >= iKB && dsize < lMB)
return string.Format("{0} KB", dsize/1024);
}
I didn't have an else in my function:
private string formatsizekb(long dsize)
{
const int iKB = 1024;
const long lMB = 1048576;
if (dsize < iKB)
return string.Format("{0} bytes", dsize);
ELSE IF (dsize >= iKB && dsize < lMB)
return string.Format("{0} KB", dsize/1024);
}
Can you be clear on what you are trying to achieve