我用CreateFile打开文件是成功的,但是我获取大小的时候失败了,请大家指教HANDLE hPartition = CreateFile("\\\\.\\i:", GENERIC_READ,FILE_SHARE_READ,0, OPEN_EXISTING, 0, 0);

DWORD dwLow, dwHigh;
dwLow = GetFileSize(hPartition,&dwHigh);//我用这个打开分区,句柄不是空的,但是返回的值是无效的,请大家指正!对了,分区都是大于2G的

解决方案 »

  1.   

    通过 GetFileSize API 函数获取文件大小;HANDLE hFile;
    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);
      

  2.   

    GetLastError看看为什么是无效的
      

  3.   

    Returns   the   size,   in   bytes,   of   a   specified   drive.   
        
      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;   
      

  4.   

    You cannot use the GetFileSize function with a handle of a nonseeking device such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function. 
      

  5.   

    用DeviceIoControl去和disk交互下得大小吧,这个应该可以得到
      

  6.   

    无效号码57: A network adapter hardware error occurred. //晕倒
    我执行的结果是错误代码 1, ERROR_INVALID_FUNCTION, 估计不支持这样用
    //获取磁盘空间可以用GetDiskFreeSpace()
      

  7.   


    GetDiskFreeSpace (strDriver.c_str(), 
    &dwSectPerClust, 
    &dwBytesPerSect,
    &dwFreeClusters, 
    &dwTotalClusters);
    unsigned __int64 nResult = dwFreeClusters*dwSectPerClust;
      

  8.   

    GetFileSize是获取文件大小的,不能获取分区大小。
    用DeviceIoControl,GET_LENGTH_INFORMATION或者IOCTL_DISK_GET_PARTITION_INFO来取。
      

  9.   

    #include <windows.h>
    #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;
    }
      

  10.   

    用GetFileSize来获取分区大小应该是不对的哦!具体应该怎么获取就看上面几楼提供的方法吧!
      

  11.   

    我自己做出来的标准答案,给以后需要解决这个问题的人做个借鉴
    ULONGLONG CSdDiskTestToolDlg::GetDiskSize(CString strDestinationFilePartition)
    {
    ULARGE_INTEGER FreeBytesAvailableToCaller;
    ULARGE_INTEGER TotalNumberOfBytes;
    ULARGE_INTEGER TotalNumberOfFreeBytes; GetDiskFreeSpaceEx(strDestinationFilePartition,
    &FreeBytesAvailableToCaller,
    &TotalNumberOfBytes,
    &TotalNumberOfFreeBytes);
    return TotalNumberOfBytes.QuadPart;
    }
    亲自验证,起码我用的是正确的