c# 如何得到文件的系统大小

c# 如何得到文件的系统大小
就是以详细信息显示文件的时候,后面的大小,file.size得到的是字节,别跟我说除以1024就行了,没那么简单,XP操作系统的文件大小四舍五入是怎么做的,求高人

bytes/1024 高搭= kb, 戚蚂拿/1024 = mb, /1024 =gb

四舍五入用Math.Round(数字,位数)

如图 我硬盘中有个文件是1.02GB,  1,096,032,256 bytes

1,096,032,256/1024/1024/1024 = 1.020759582519531...

Math.Round(1.020759582519531,2)  得到物枣1.02


/// 
/// Function to convert the given bytes to either Kilobyte, Megabyte, or Gigabyte
///

/// Double -> Total bytes to be converted
/// String -> Type of conversion to perform
/// Int32 -> Converted bytes
///
public static double ConvertSize(double bytes, string type)
{
try
{
const int CONVERSION_VALUE = 1024;
//determine what conversion they want
switch (type)
{
case "BY":
//convert to bytes (default)
return bytes;
break;
case "KB":
//convert to kilobytes
return (bytes / CONVERSION_VALUE);
break;
case "MB":
//convert to megabytes
return (bytes / CalculateSquare(CONVERSION_VALUE));
break;
case "GB":
//convert to gigabytes
return (bytes / CalculateCube(CONVERSION_VALUE));
break;
default:
//default
return bytes;
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
/// 宴裂山
/// Function to calculate the square of the provided number
///

/// Int32 -> Number to be squared
/// Double -> THe provided number squared
///
public static double CalculateSquare(Int32 number)
{
return Math.Pow(number, 2);
}
///
/// Function to calculate the cube of the provided number
///

/// Int32 -> Number to be cubed<晌中/param>
/// Double ->源丛 THe provided number cubed
///
public static double CalculateCube(Int32 number)
{
return Math.Pow(number, 3);
}
//Sample Useage
String Size = "File is " + ConvertSize(250222,"MB") + " Megabytes in size"

有什么证据证明不是除以1024?

KB = B >> 10

MB = KB >> 10 = B >> 20

GB = MB >> 10 = KB >> 20 = B >> 30

计算的区别主要是由于硬件厂商使用1000进制,操作系统使用1024造成的。


← 上一篇: 下一篇: →