ファイルのサイズを 1024 から 1kb に解析するにはどうすればよいですか? if でいっぱいの 30 行のような関数を作成したことがあります。それを行うためのより「エレガントな」方法はありますか?何を使用する必要がありますか?1kb = 1000b または 1kb = 1024b?
2099 次
5 に答える
7
この解決策は不合理に見えません。少し長いですが、エクサ/ペタバイトに対応しています!
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;
}
サンプル使用法
上記の関数を public static メソッドとしてヘルパーまたはユーティリティ クラスに配置することをお勧めします。
// 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 に答える