File size - actual number of bytes used on disk (not allocated) [duplicate]

和自甴很熟 提交于 2019-12-24 08:25:15

问题


Possible Duplicate:
Get Size of file on disk

Is there a way to retrieve the actual number of bytes used for a particular file on disk, using C# / windows?

My application implements "watch" folders, similar to FileSystemWatcher. Some watch folders exist on shared storage (both network and fibre channel SAN), others on locally attached storage.

Files are copied into a watch folder by processes that are completely out of my control, sized anywhere from 1 GB to > 500 GB. Because of the nature of shared file systems, hoping for an exception when opening files "exclusively" (FileMode.Open, FileAccess.Read, FileShare.None) doesn't work either.

These watch folders are not supposed to take action until a file is completely copied/closed - otherwise problems downstream can occur.

To get the "real" file size used on disk I have tried:

  • System.IO.FileInfo
  • GetFileSizeEx (kernel32 p/invoke)
  • FindFirstFileEx (kernel32 p/invoke)
  • GetCompressedFileSize & GetDiskFreeSpace (kernel32 p/invoke)

Any suggestions would be very much appreciated. It seems like I'm dealing with a limitation of the Windows OS?


回答1:


To summarize, the issue here is that the other applications copy the file by creating the destination, then using SetFileSize to set the file size to the final size (let's say 1GB), essentially creating a 1GB file filled completely with zeroes.

The applications then seek back to the beginning of the file and start filling it sequentially with data. The goal is to determine what point in the "filling it with data" the other applications have reached.

The unfortunate answer is that there is no way to obtain this information. As far as the file system is concerned, the file exists and its size is 1GB. it so happens that the file is being actively updated without changing the size, but those internal changes are not audited in file metadata. There is no "last modified time" for individual bytes of a file.

If you have domain-specific knowledge of the files (e.g. you know that the last byte is never zero) you can poll the last byte of the file and back off if it is zero.




回答2:


There's no API to get the "size on disk" of a file, you have to calculate it yourself based on your knowledge of the disk geometry.




回答3:


Using GetDiskFreeSpace you can get enough information to calculate the cluster size. Round up to this value to get the actual size on disk, or a good approximation of it.



来源:https://stackoverflow.com/questions/13959228/file-size-actual-number-of-bytes-used-on-disk-not-allocated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!