以编程方式确定文件“磁盘上的大小” 提前
在写入之前,我需要知道给定的内存缓冲区作为磁盘(USB 棒)文件有多大。 我知道除非大小落在块大小边界上,否则它可能会向上舍入,例如 1 字节文件在磁盘上占用 4096 字节。 我目前正在使用 GetDiskFreeSpace() 来计算磁盘块大小,然后使用它来计算磁盘大小,如下所示:
GetDiskFreeSpace(szDrive, &dwSectorsPerCluster,
&dwBytesPerSector, NULL, NULL);
dwBlockSize = dwSectorsPerCuster * dwBytesPerSector;
if (dwInMemorySize % dwBlockSize != 0)
{
dwSizeOnDisk = ((dwInMemorySize / dwBlockSize) * dwBlockSize) + dwBlockSize;
}
else
{
dwSizeOnDisk = dwInMemorySize;
}
这似乎工作正常,但是GetDiskFreeSpace() 仅适用于最大 2GB 的磁盘。 GetDiskFreeSpaceEx() 不会返回相同的信息,所以我的问题是,我还能如何计算大于 2GB 的驱动器的此信息? 是否有我错过的 API 调用? 我可以根据整体磁盘大小假设一些硬值吗?
I need to know how big a given in-memory buffer will be as an on-disk (usb stick) file before I write it. I know that unless the size falls on the block size boundary, its likely to get rounded up, e.g. a 1 byte file takes up 4096 bytes on-disk. I'm currently doing this using GetDiskFreeSpace() to work out the disk block size, then using this to calculate the on-disk size like this:
GetDiskFreeSpace(szDrive, &dwSectorsPerCluster,
&dwBytesPerSector, NULL, NULL);
dwBlockSize = dwSectorsPerCuster * dwBytesPerSector;
if (dwInMemorySize % dwBlockSize != 0)
{
dwSizeOnDisk = ((dwInMemorySize / dwBlockSize) * dwBlockSize) + dwBlockSize;
}
else
{
dwSizeOnDisk = dwInMemorySize;
}
Which seems to work fine, BUT GetDiskFreeSpace() only works on disks up to 2GB according to MSDN. GetDiskFreeSpaceEx() doesn't return the same information, so my question is, how else can I calculate this information for drives >2GB? Is there an API call I've missed? Can I assume some hard values depending on the overall disk size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MSDN 仅声明
GetDiskFreeSpace()
函数无法报告大于 2GB 的卷大小。 它可以很好地检索每个簇的扇区和每个扇区的字节数,我自己也用它来编写看起来非常相似的代码;-)但是如果您也想要磁盘容量,则需要额外调用
GetDiskFreeSpaceEx()
。MSDN only states that the
GetDiskFreeSpace()
function cannot report volume sizes greater than 2GB. It works fine for retrieving sectors per cluster and bytes per sector, I've used it myself for very similar-looking code ;-)But if you want disk capacity too, you'll need an additional call to
GetDiskFreeSpaceEx()
.磁盘上文件的大小是一个模糊的概念。 在 NTFS 中,文件由一组数据元素组成。 您首先想到的是“未命名的数据流”。 这是文件的一个属性,如果文件很小,可以与目录项中的其他属性一起打包。 显然,最多可以存储700-800个数据流目录项本身的字节。 因此,假设的 1 字节文件将与 0 字节或 700 字节文件一样大。
另一个影响是文件压缩。 这将使磁盘上的大小可能小于内存中的大小。
The size of a file on disk is a fuzzy concept. In NTFS, a file consists of a set of data elements. You're primarilty thinking of the "unnamed data stream". That's an attribute of a file that, if small, can be packed with the other attributes in the directory entry. Apparently, you can store a data stream of up to 700-800 bytes in the directory entry itself. Hence, your hypothetical 1 byte file would be as big as a 0 byte or 700 byte file.
Another influence is file compression. This will make the on-disk size potentially smaller than the in-memory size.
您应该能够使用 DeviceIoControl 函数获取此信息,并且
DISK_GEOMETRY_EX。 它将返回一个包含您正在查找的信息的结构,我认为
http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms809010.aspx
You should be able to obtain this information using the DeviceIoControl function and
DISK_GEOMETRY_EX. It will return a structure that contains the information you are looking for I think
http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms809010.aspx
在动作脚本中!
In actionscript!