Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
186 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 600 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 1400 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 1400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

772 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