以字节和MB为单位显示
本文关键字:为单位 显示 MB 字节 | 更新日期: 2023-09-27 18:11:45
我尝试使IF条件出现在我的MessageBox中,如果freeSpaceInC/(1000000)<1024
和>1024
出现在MB中,则以字节为单位的值。
我有下一个代码,但它不能更简单吗?以及如何识别下一个如果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();
}
}
你可以更简单:
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";
}
或者您可以使用类似的通用解决方案:
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;
}
为它创建一个类,并将翻译表设置为静态