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
178 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

22 Experts available now in Live!

Get 1:1 Help Now