Link to home
Start Free TrialLog in
Avatar of wally_davis
wally_davisFlag for United States of America

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
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);
        }

Open in new window

SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry I guess that was not your question.
Can you be clear on what you are trying to achieve
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wally_davis

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.formatsizekb(long)': 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.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
        }