怎样得到磁盘的大小,比如c盘(d盘)的大小?谢谢!

解决方案 »

  1.   

    GetDiskFreeSpace
    The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk. The GetDiskFreeSpace function cannot report volume sizes that are greater than 2 GB. To ensure that your application works with large capacity hard drives, use the GetDiskFreeSpaceEx function. BOOL GetDiskFreeSpace(
      LPCTSTR lpRootPathName,          // root path
      LPDWORD lpSectorsPerCluster,     // sectors per cluster
      LPDWORD lpBytesPerSector,        // bytes per sector
      LPDWORD lpNumberOfFreeClusters,  // free clusters
      LPDWORD lpTotalNumberOfClusters  // total clusters
    );
    Parameters
    lpRootPathName 
    [in] Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with a trailing backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\. However, a drive specification such as "C:" must have a trailing backslash. 
    Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping. Windows 95 OSR2 and later: UNC paths are supported. lpSectorsPerCluster 
    [out] Pointer to a variable for the number of sectors per cluster. 
    lpBytesPerSector 
    [out] Pointer to a variable for the number of bytes per sector. 
    lpNumberOfFreeClusters 
    [out] Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread. 
    Windows 2000/XP: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk. lpTotalNumberOfClusters 
    [out] Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread. 
    Windows 2000/XP: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk. Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
    The GetDiskFreeSpaceEx function lets you avoid some of the arithmetic required by the GetDiskFreeSpace function.Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available beginning with Windows 95 OEM Service Release 2 (OSR2), and you should use it whenever possible. The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are larger than 2 gigabytes. Windows 95: For volumes that are larger than 2 gigabytes, the GetDiskFreeSpace function may return misleading values. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size.Windows 95/98/Me: GetDiskFreeSpaceW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.Requirements 
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Winbase.h; include Windows.h.
      Library: Use Kernel32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.See Also
    File I/O Overview, File I/O Functions, GetDiskFreeSpaceEx, GetDriveType 
      

  2.   

    我们希望获得系统所有磁盘的信息,包括软盘,硬盘,光盘等等;
      GetLogicalDriveStrings(dwBufferLength,lpBuffer);
      //获得逻辑设备的信息;
      GetVolumeInformation(lpRootPathName,lpVolumeNameBuffer,
       dwVolumeNameSize,&dwVolumeSerialNumber,
       &dwMaximumComponentLength,&dwFileSystemFlags,
          lpFileSystemNameBuffer,dwFileSystemNameSize);
      //获得磁盘卷信息,包括卷名称和格式类型;
      GetDiskFreeSpaceEx(lpRootPathName,&FreeBytesAvailable,
              &TotalNumberOfBytes,&TotalNumberOfFreeBytes);
      //探测磁盘的空间使用情况;
      

  3.   

    BOOL GetDiskFreeSpaceEx(
      LPCTSTR lpDirectoryName,                 // directory name
      PULARGE_INTEGER lpFreeBytesAvailable,    // bytes available to caller
      PULARGE_INTEGER lpTotalNumberOfBytes,    // bytes on disk
      PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk
    );
      

  4.   

    GetDiskFreeSpace()是得到磁盘扇区等信息
    BOOL GetDiskFreeSpace(
      LPCTSTR lpRootPathName,          // root path
      LPDWORD lpSectorsPerCluster,     // sectors per cluster
      LPDWORD lpBytesPerSector,        // bytes per sector
      LPDWORD lpNumberOfFreeClusters,  // free clusters
      LPDWORD lpTotalNumberOfClusters  // total clusters
    );GetDiskFreeSpaceEx()是得到磁盘空间等信息
    BOOL GetDiskFreeSpaceEx(
      LPCTSTR lpDirectoryName,                 // directory name
      PULARGE_INTEGER lpFreeBytesAvailable,    // bytes available to caller
      PULARGE_INTEGER lpTotalNumberOfBytes,    // bytes on disk
      PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk
    );
      

  5.   

    CString CInfoshowDlg::GetDisk(char *disk)
    {
     _ULARGE_INTEGER result_freespace,result_totalspace,result_g;
     CString disk_space;
     int error = 0;
     int free_space,total_space;
     int free_percent; /* Get free space */
     GetDiskFreeSpaceEx(disk,&result_freespace,&result_totalspace,&result_g);
     if (error != 0)
     {
      CString t;
      t.Format("GetDiskFreeSpace() error=%d",error);
      AfxMessageBox(t);
     } free_space = result_freespace.QuadPart/1024/1024;
     total_space = result_totalspace.QuadPart/1024/1024;
     free_percent = (float)free_space/total_space*100;
     disk_space.Format("%dM/ %dM (%d%%)",free_space,total_space,free_percent); return disk_space;
    }
      

  6.   

    给你一个以字节为单位的结果:
    LPCWSTR  lpDirectoryName = L"C:\\";
    ULARGE_INTEGER   FreeBytesAvailableToCaller;
    ULARGE_INTEGER   TotalNumberOfBytes;
    ULARGE_INTEGER   TotalNumberOfFreeBytes;
    ZeroMemory(&FreeBytesAvailableToCaller,sizeof(PULARGE_INTEGER));
    ZeroMemory(&TotalNumberOfBytes,sizeof(PULARGE_INTEGER));
    ZeroMemory(&TotalNumberOfFreeBytes,sizeof(PULARGE_INTEGER));
    ::GetDiskFreeSpaceExW(lpDirectoryName,&FreeBytesAvailableToCaller,
    &TotalNumberOfBytes,&TotalNumberOfFreeBytes); LONGLONG FreeByteAvailable = FreeBytesAvailableToCaller.QuadPart;
     LONGLONG TotalNumber       = TotalNumberOfBytes.QuadPart;
     LONGLONG TotalFreeBytes    = TotalNumberOfFreeBytes.QuadPart;
    如果需要格式化为MB,则像楼上那样格式化