我用CreateFile打开文件是成功的,但是我获取大小的时候失败了,请大家指教HANDLE hPartition = CreateFile("\\\\.\\i:", GENERIC_READ,FILE_SHARE_READ,0, OPEN_EXISTING, 0, 0);
DWORD dwLow, dwHigh;
dwLow = GetFileSize(hPartition,&dwHigh);//我用这个打开分区,句柄不是空的,但是返回的值是无效的,请大家指正!对了,分区都是大于2G的
DWORD dwLow, dwHigh;
dwLow = GetFileSize(hPartition,&dwHigh);//我用这个打开分区,句柄不是空的,但是返回的值是无效的,请大家指正!对了,分区都是大于2G的
DWORD dwFileSize = 0;
char szData[30];hFile = CreateFile("e:\test1.wav",GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);dwFileSize = GetFileSize(hFile,NULL);CloseHandle(hFile);sprintf(szData,"File Size : %d",dwFileSize);MessageBox(NULL,szData,"FileSize",MB_ICONINFORMATION);
Unit
SysUtils
Category
file management routines
Delphi syntax:
function DiskSize(Drive: Byte): Int64;
C++ syntax:
extern PACKAGE __int64 __fastcall DiskSize(Byte Drive);
Description
DiskSize returns the size in bytes of the specified drive, where 0 = Current, 1 = A, 2 = B, etc. DiskSize returns -1 if the drive number is invalid.
Note: DiskSize is only available on Windows.
This example uses a form with a label on it. When the following code executes, it displays a message in the label indicating the number of KB free, and what percentage of the entire disk space that represents.
var
S: string;
AmtFree: Int64;
Total: Int64;
begin
AmtFree := DiskFree(0);
Total := DiskSize(0);
S := IntToStr(AmtFree div Total) + 'percent of the space on drive 0 is free: ' (AmtFree div 1024) + ' Kbytes free. ';
Label1.Caption := S;
end;
我执行的结果是错误代码 1, ERROR_INVALID_FUNCTION, 估计不支持这样用
//获取磁盘空间可以用GetDiskFreeSpace()
GetDiskFreeSpace (strDriver.c_str(),
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
unsigned __int64 nResult = dwFreeClusters*dwSectPerClust;
用DeviceIoControl,GET_LENGTH_INFORMATION或者IOCTL_DISK_GET_PARTITION_INFO来取。
#include <stdio.h>typedef BOOL (WINAPI *PGETDISKFREESPACEEX)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive)
{
PGETDISKFREESPACEEX pGetDiskFreeSpaceEx;
__int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
BOOL fResult;
pGetDiskFreeSpaceEx = (PGETDISKFREESPACEEX)GetProcAddress(
GetModuleHandle("kernel32.dll"),
"GetDiskFreeSpaceExA");
if (pGetDiskFreeSpaceEx)
{
fResult = pGetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
// Process GetDiskFreeSpaceEx results.
if (fResult)
{
printf("Free Bytes To Caller = %I64d (%dG)\n", i64FreeBytesToCaller, i64FreeBytesToCaller / 1024 / 1024 / 1024);
printf("Total bytes = %I64d (%dG)\n", i64TotalBytes, i64TotalBytes / 1024 / 1024 / 1024);
printf("Total free bytes = %I64d (%dG)\n", i64FreeBytes, i64FreeBytes / 1024 / 1024 / 1024);
}
return fResult;
}
else
{
fResult = GetDiskFreeSpaceA (pszDrive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
// Process GetDiskFreeSpace results.
if(fResult)
{
printf("Total free bytes = %I64d\n",
dwFreeClusters*dwSectPerClust*dwBytesPerSect);
}
return fResult;
}
}int main(int argc, char *argv[])
{
MyGetDiskFreeSpaceEx("C:"); return 0;
}
ULONGLONG CSdDiskTestToolDlg::GetDiskSize(CString strDestinationFilePartition)
{
ULARGE_INTEGER FreeBytesAvailableToCaller;
ULARGE_INTEGER TotalNumberOfBytes;
ULARGE_INTEGER TotalNumberOfFreeBytes; GetDiskFreeSpaceEx(strDestinationFilePartition,
&FreeBytesAvailableToCaller,
&TotalNumberOfBytes,
&TotalNumberOfFreeBytes);
return TotalNumberOfBytes.QuadPart;
}
亲自验证,起码我用的是正确的