C# 从字节数组中获取文件的大小(不保存到光盘)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13669480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get file's size from bytes array (without saving to disc)
提问by nKognito
I have a byte's array and I want to calculate what would be the file size if I'll write these bytes to file. Is it possible without writing the file to disc?
我有一个字节数组,如果我将这些字节写入文件,我想计算文件大小。是否可以不将文件写入光盘?
采纳答案by Sergey Berezovskiy
What about array.Length? Looks like a size in bytes.
怎么样array.Length?看起来像字节大小。
回答by Jon Skeet
Um, yes:
嗯,是的:
int length = byteArray.Length;
A byte in memory would be a byte on disk... at least in higher level file system terms. You also need to potentially consider how many individual blocks/clusters would be used (and overhead for a directory entry), and any compression that the operating system may supply, but it's not clear from the question whether that's what you're after.
内存中的一个字节将是磁盘上的一个字节......至少在更高级别的文件系统术语中。您还可能需要考虑将使用多少个单独的块/集群(以及目录条目的开销),以及操作系统可能提供的任何压缩,但从问题中不清楚这是否是您所追求的。
If you really dowant to know the "size on disk" as opposed to the file size (in the same way that Windows can show the two numbers) I suspect you'd genuinely have to write it to disk - and then use a Win32 API to find out the actual size on disk.
如果你真的不想要知道,而不是文件大小(以同样的方式,Windows可以显示两个数字)的“磁盘上的大小”我怀疑你真的必须把它写入到磁盘-然后用一个Win32 API 来找出磁盘上的实际大小。
回答by LittleSweetSeas
Array.Lengthwould give your total size expressed in byte.
Physical dimension on disk may be a little bit more considering cluster size.
Array.Length会给你的总大小以字节表示。
考虑到集群大小,磁盘上的物理维度可能会更多一点。
回答by Walter Vehoeven
some time ago I found this snipped and since then I like to do this
前段时间我发现这被剪断了,从那时起我就喜欢这样做
public static string GetSizeInMemory(this long bytesize)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = Convert.ToDouble(bytesize);
int order = 0;
while(len >= 1024D && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
return string.Format(CultureInfo.CurrentCulture,"{0:0.##} {1}", len, sizes[order]);
}
You can use this extension method when accessing anything that provides file or memory size like a FileInfo or a Process, bellow are 2 samples
在访问任何提供文件或内存大小(如 FileInfo 或进程)时,您可以使用此扩展方法,下面是 2 个示例
private void ValidateResources(object _)
{
Process p = Process.GetCurrentProcess();
double now = p.TotalProcessorTime.TotalMilliseconds;
double cpuUsage = (now - processorTime) / MONITOR_INTERVAL;
processorTime = now;
ThreadPool.GetMaxThreads(out int maxThreads, out int _);
ThreadPool.GetAvailableThreads(out int availThreads, out int _);
int cpuQueue = maxThreads - availThreads;
var displayString= string.Concat(
p.WorkingSet64.GetSizeInMemory() + "/"
, p.PeakWorkingSet64.GetSizeInMemory(), " RAM, "
, p.Threads.Count, " threads, ", p.HandleCount.ToString("N0", CultureInfo.CurrentCulture)
, " handles, ", Math.Min(cpuUsage, 1).ToString("P2", CultureInfo.CurrentCulture)
, " CPU usage, ", cpuQueue, " CPU queue depth"
));
}
or with a file
或者用一个文件
FileInfo file = new FileInfo(pathtoFile);
string displaySize= file.Length.GetSizeInMemory();

