作者:傲慢的小草7_170 | 来源:互联网 | 2023-02-05 10:25
我用C语言计算文件大小: -
FileInfo info = new FileInfo(file);
uint dummy, sectorsPerCluster, bytesPerSector;
int result = GetDiskFreeSpaceW(info.Directory.Root.FullName,
out sectorsPerCluster, out bytesPerSector, out dummy, out dummy);
if (result == 0) throw new Win32Exception();
uint clusterSize = sectorsPerCluster * bytesPerSector;
uint hosize;
uint losize = GetCompressedFileSizeW(file, out hosize);
long size;
size = (long)hosize <<32 | losize;
var x = (((size + clusterSize - 1) / clusterSize) * clusterSize); // in bytes
但是当我尝试将其转换为GB时: -
x/ (1024 * 1024 * 1024)
我总是得到0作为答案.我假设这与x的数据类型有关.有人可以帮我理解这个吗?
1> Joel Coehoor..:
它正在进行整数除法.对于任何小于1GB的东西你都会看到0,甚至超过你还会看到整数.试试这个:
x/ (1024.0 * 1024.0 * 1024.0)
要么
x/ (1024D * 1024D * 1024D)
并确保将结果放入支持浮点值的内容中.可能你可以改变:
var x =
至
double x =