Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

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