我尝试制作IF条件,在我的MessageBox中显示,如果freeSpaceInC/(1000000)<1024
以及>1024
是if (dialogResult == DialogResult.Yes)
以MB显示,则以字节为单位。
我有下一个代码,但它不能变得更简单?以及如何识别下一个if 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();
}
}
:
$(document).on("click",function(e){
var $target = $(e.target);
var isTargetClick = !$target.add($target.parents()).is(".btn");
if(isTargetClick){
$(this).find('i').attr("class",'glyphicon glyphicon-asterisk').css( "color", "black" );
}
});
答案 0 :(得分:3)
你可以更简单:
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";
}
答案 1 :(得分:1)
或者你可以使用类似的东西去寻找通用的解决方案:
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;
}
制作一个类并使转换表保持静态。