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
181 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Help Top 1 and Distinct? 6 49
Add a Condition in ASP Repeater 6 48
jquery tab header text 1 28
.Net remove carriage returns, line feeds and tabbs 5 18
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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