Solved

Need help on how to use a routine I was given in C# to format file sizes to KB or MB.

Posted on 2008-10-21
6
177 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:wally_davis
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 150 total points
ID: 22769641
double lenInByte = fi.Length;
double lenInKB = lenInByte/1024;
double lenInMB = lenInKB/1024;
lvsi.Text = Convert.ToString(lenInKB);
or
lvsi.Text = Convert.ToString(lenInMB);

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22769714
Sorry I guess that was not your question.
Can you be clear on what you are trying to achieve
0
 
LVL 2

Accepted Solution

by:
MasterPage earned 350 total points
ID: 22769724
Hi Wally,

You would just need to pass fi.Length to formatsizekb.
string s = formatsizekb(fi.Length)

BTW, you should make the dsize parameter in formatsizekb a long rather than a double.  Double is a floating point, but f.Length will always be an integer (long):
private string formatsizekb(long lsize)

For commas, replace the {0} in your format strings with {0:#,#}:
return string.Format("{0:#,#} bytes", lsize);

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:wally_davis
ID: 22770110
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.

0
 
LVL 2

Assisted Solution

by:MasterPage
MasterPage earned 350 total points
ID: 22770422
Can you show me your code where you're calling formatsizekb?

Your second error is because you need an else after your second if statement in formatsizekb.  It has to know what to return if the two if's are false, that is if lsize >= 1MB.

private string formatsizekb(long lsize)

        {

            const int iKB = 1024;

            const long lMB = 1048576;

 

            if (lsize < iKB)

                return string.Format("{0:#,#} bytes", lsize);

            if (lsize >= iKB && lsize < lMB)

                return string.Format("{0:#,#} KB", lsize / 1024);

            else

                ???

        }

Open in new window

0
 

Author Comment

by:wally_davis
ID: 22770439
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);
        }
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now