uid
uid

Reputation: 347

Display the values in Bytes and MB

I try to make an IF condition, to appear in my MessageBox, the value in Bytes if the freeSpaceInC/(1000000)<1024 and if it is >1024 to appear in MB.

I have the next code, but It can't be made ​​simpler ? and how I can make to recognize the next if if (dialogResult == DialogResult.Yes):

static void Main()
{    
    var drive = new DriveInfo("c");
    long freeSpaceInC = drive.TotalFreeSpace;

    var drive1 = new DriveInfo("d");
    long freeSpaceInD = drive.TotalFreeSpace;

    if ((freeSpaceInC / (1000000) < 1024) && (freeSpaceInD / (1000000) < 1024))
    {
        DialogResult dialogResult = MessageBox.Show("There is " + freeSpaceInC / (1000000) + "B free in C: and " + freeSpaceInD / (1000000) + "B free in D:. Do you want to continue the installation?", "MATLAB_R2008a_ENU_EU", MessageBoxButtons.YesNo);
    }
    else if ((freeSpaceInC / (1000000) > 1024) && (freeSpaceInD / (1000000) > 1024))
    {
        DialogResult dialogResult = MessageBox.Show("There is " + freeSpaceInC / (1000000) + "MB free in C: and " + freeSpaceInD / (1000000) + "MB free in D:. Do you want to continue the installation?", "MATLAB_R2008a_ENU_EU", MessageBoxButtons.YesNo);
    }
    else if ((freeSpaceInC / (1000000) > 1024) && (freeSpaceInD / (1000000) < 1024))
    {
        DialogResult dialogResult = MessageBox.Show("There is " + freeSpaceInC / (1000000) + "MB free in C: and " + freeSpaceInD / (1000000) + "B free in D:. Do you want to continue the installation?", "MATLAB_R2008a_ENU_EU", MessageBoxButtons.YesNo);
    }
    else if ((freeSpaceInC / (1000000) < 1024) && (freeSpaceInD / (1000000) > 1024))
    {
        DialogResult dialogResult = MessageBox.Show("There is " + freeSpaceInC / (1000000) + "B free in C: and " + freeSpaceInD / (1000000) + "MB free in D:. Do you want to continue the installation?", "MATLAB_R2008a_ENU_EU", MessageBoxButtons.YesNo);
    }

    if (dialogResult == DialogResult.Yes)
    {
        Form1 fm1 = new Form1();
        fm1.ShowDialog();
        fm1.Close();
    }            
    else if (dialogResult == DialogResult.No)
    {
        Application.Exit();
    }
}

Upvotes: 0

Views: 174

Answers (2)

SOReader
SOReader

Reputation: 6017

Or you could go to a generic solution with something similar to this:

string TranslateSize(long value) {
    var table =
        Enumerable
        .Range(0, 5)
        .Select(x => {
            var @base = (long)Math.Pow(1024, x);
            return new {
                Start = x != 0 ? @base : 0,
                End = 1024 * @base,
                Divider = @base,
                Suffix = ""
            };})
        .Zip(
            new []{"B", "kB", "MB", "GB", "TB"},
            (l, r) => new {
                l.Start,
                l.End,
                l.Divider,
                Suffix = r
            });

    var result = table.Single(x => x.Start <= value && value < x.End);
    return result;
}

Make a class of it and make the translation table static.

Upvotes: 1

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You can go way simpler:

static void Main()
{    
    DialogResult dialogResult = MessageBox.Show("There is " + toReadableSize(freeSpaceInC) + " free in C: and " + toReadableSize(freeSpaceInD) + " free in D:. Do you want to continue the installation?", "MATLAB_R2008a_ENU_EU", MessageBoxButtons.YesNo);

    if (dialogResult == DialogResult.Yes)
    {
        Form1 fm1 = new Form1();
        fm1.ShowDialog();
        fm1.Close();
    }            
    else if (dialogResult == DialogResult.No)
    {
        Application.Exit();
    }
}

private static string toReadableSize(int size)
{
    if(size < 1024)
        return size + "B";

    if(size < 1024*1024)
        return Math.Round(((float)size / 1024), 2) + "KB";

    if(size < 1024*1024*1024)
        return Math.Round(((float)size / (1024*1024)), 2) + "MB";

    return Math.Round(((float)size / (1024*1024*1024)), 2) + "GB";
}

Upvotes: 3

Related Questions