有没有一种方法可以使用通用逻辑来检索磁盘上的文件大小,而不管底层操作系统如何?以下代码适用于Windows,但显然不适用于Linux.
public static long GetFileSizeOnDisk(string file)
{
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;
return ((size + clusterSize - 1) / clusterSize) * clusterSize;
}
[DllImport("kernel32.dll")]
static extern uint GetCompressedFileSizeW([In,MarshalAs(UnmanagedType.LPWStr)] string lpFileName,[Out,MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);
[DllImport("kernel32.dll",SetLastError = true,PreserveSig = true)]
static extern int GetDiskFreeSpaceW([In,MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName,out uint lpSectorsPerCluster,out uint lpBytesPerSector,out uint lpNumberOfFreeClusters,out uint lpTotalNumberOfClusters);
或者,我正在寻找一种适用于Linux的类似实现.谁能指出我正确的方向?
谢谢你的帮助 !
最佳答案
原文链接:https://www.f2er.com/linux/439834.html