也许有点晚了,但是这里有你所需的关于文件大小、瓦特等方面的一切。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
public static string FormatSize(string unit, double size, int order, int decimal_places, bool add_space) {
string[] suffixes = new string[] {"", "K","M","G","T","P","E","Z","Y"};
int exponent = order - 8 > 0 ? order - 8 : 0;
order -= exponent;
string suffix = suffixes[order];
while (order > 0) {
size /= 1024;
order--;
}
string sDecimals = new String('0', decimal_places);
string sExponent = exponent != 0 ? "E" + exponent : "";
string dot = decimal_places > 0 ? "." : "";
return size.ToString("#,##0" + dot + sDecimals + sExponent) + (add_space ? " " : "") + suffix + unit;
}
public static void Main(string[] Args)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string sz;
sz = FormatSize("B", 1024, 1, 0, false);
Console.WriteLine(sz);
sz = FormatSize("B", 1024*1024 + 512, 1, 0, true);
Console.WriteLine(sz);
sz = FormatSize("W", 1024 * 1024 + 512, 1, 2, true);
Console.WriteLine(sz);
sz = FormatSize("B", 1024 * 1024 + 512, 2, 2, true);
Console.WriteLine(sz);
sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2, 3, 0, false);
Console.WriteLine(sz);
sz = FormatSize("bit", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2, 3, 1, false);
Console.WriteLine(sz);
sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 2, false);
Console.WriteLine(sz);
sz = FormatSize("Ω", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 1, false);
Console.WriteLine(sz);
sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 10000000, 3, 2, false);
Console.WriteLine(sz);
sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 0, false);
Console.WriteLine(sz);
sz = FormatSize("bit", 1208925819614629174706176f, 9, 2, true);
Console.WriteLine(sz);
}
}
}
这里是一些输出:
1KB
1,025 KB
1,024.50 KW
1.00 MB
2GB
1.5Gbit
1.50GB
1.5GΩ
1.49GB
1GB
1.00E1 Ybit