如何解析从 1024 到 1kb 的文件大小?一旦我为它创建了一个函数,它就像 30 行长的 if 一样。有没有更“优雅”的方式来做到这一点?我需要使用什么?1kb = 1000b 还是 1kb = 1024b?
问问题
2099 次
5 回答
7
这个解决方案看起来并不合理。它有点长,但它确实适合 exa/PB !
C# 人类可读的文件大小函数
// Returns the human-readable file size for an arbitrary, 64-bit file size
// The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
public static string GetSizeReadable(long i)
{
string sign = (i < 0 ? "-" : "");
double readable = (i < 0 ? -i : i);
string suffix;
if (i >= 0x1000000000000000) // Exabyte
{
suffix = "EB";
readable = (double)(i >> 50);
}
else if (i >= 0x4000000000000) // Petabyte
{
suffix = "PB";
readable = (double)(i >> 40);
}
else if (i >= 0x10000000000) // Terabyte
{
suffix = "TB";
readable = (double)(i >> 30);
}
else if (i >= 0x40000000) // Gigabyte
{
suffix = "GB";
readable = (double)(i >> 20);
}
else if (i >= 0x100000) // Megabyte
{
suffix = "MB";
readable = (double)(i >> 10);
}
else if (i >= 0x400) // Kilobyte
{
suffix = "KB";
readable = (double)i;
}
else
{
return i.ToString(sign + "0 B"); // Byte
}
readable = readable / 1024;
return sign + readable.ToString("0.### ") + suffix;
}
示例使用
建议将上述函数作为公共静态方法放在帮助程序或实用程序类中。
// EXAMPLE OUTPUT
GetSizeReadable(1023); // 1023 B
GetSizeReadable(1024); // 1 KB
GetSizeReadable(1025); // 1.001 KB
// Example of getting a file size and converting it to a readable value
string fileName = "abc.txt";
long fileSize = new System.IO.FileInfo(fileName).Length;
string sizeReadable = GetSizeReadable(fileSize);
于 2012-08-15T07:44:33.567 回答
5
也许是这样的?
public string FileSizeAsString(long lengthOfFile)
{
string[] sizes = { "bytes", "KB", "MB", "GB" };
int j = 0;
while (lengthOfFile > 1024 && j < sizes.Length)
{
lengthOfFile = lengthOfFile / 1024;
j++;
}
return (lengthOfFile + " " + sizes[j]);
}
用法:
Console.WriteLine(FileSizeAsString(new FileInfo(@"C:\\your_file_here.ext").Length));
您可以根据需要扩展字符串数组sizes
,它将继续计算。
于 2012-08-15T07:49:38.950 回答
3
并不是说您需要很多(模糊)类似的方式,但是,鉴于尚未有人提出建议(并且为了一点老式的乐趣),您可以这样做...
// Note: StrFormatByteSize truncates as opposed to rounds (so, 1.998 becomes 1.99, not 2.00)
[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
public static extern long StrFormatByteSize(long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize);
private static String FormatBytes(Int64 bytes)
{
StringBuilder sb = new StringBuilder(10);
StrFormatByteSize(bytes, sb, 10);
return sb.ToString();
}
...或者我已经提供的答案版本...
private static String FormatBytes(Int64 bytes)
{
const Int64 KB = 1024,
MB = KB * 1024,
GB = MB * 1024,
TB = GB * 1024L,
PB = TB * 1024L,
EB = PB * 1024L;
if (bytes < KB) return bytes.ToString("N0") + " Bytes";
if (bytes < MB) return Decimal.Divide(bytes, KB).ToString("N") + " KB";
if (bytes < GB) return Decimal.Divide(bytes, MB).ToString("N") + " MB";
if (bytes < TB) return Decimal.Divide(bytes, GB).ToString("N") + " GB";
if (bytes < PB) return Decimal.Divide(bytes, TB).ToString("N") + " TB";
if (bytes < EB) return Decimal.Divide(bytes, PB).ToString("N") + " PB";
return Decimal.Divide(bytes, EB).ToString("N") + " EB";
}
于 2012-08-15T08:47:28.927 回答
2
这个怎么样:
public string FormatSize(long size)
{
double result = size;
var sizes = new string[] { "", "K", "M", "G", "T", "P", "E" };
int i = 0;
while (result > 1000 && i < sizes.Length)
{
result /= 1000; // or 1024 if you want
i++;
}
// EDIT: Optimized decimal places
string format = "{0:0.00} {1}B"; // default: 2 decimals
switch (sizes[i])
{
case "":
format = "{0} B"; // no decimals for bytes
break;
case "K":
format = "{0:0.0} KB"; // 1 decimal for KB
break;
}
// /EDIT
return string.Format(format, result, sizes[i]);
}
于 2012-08-15T07:49:43.513 回答
0
int fileSize = 10485258;//size of your file
string size = fileSize >= 1024*1024 ?
(fileSize / 1024 / 1024).ToString()+"MB" :
(fileSize >= 1024 ? (fileSize / 1024).ToString() + "KB" : fileSize.ToString() + "B");
MessageBox.Show(size);
于 2012-08-15T07:49:43.133 回答