码迷,mamicode.com
首页 > 其他好文 > 详细

获取磁盘分区信息

时间:2021-01-26 11:48:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sizeof   byte   amp   www   line   打印   log   col   winapi   

1、封装一个函数 bool CheckDiskExist(LPCTSTR lpszDrive) 判断指定的盘符是否存在;
2、调用 GetLogicalDrives 获得当前系统有几个分区,分别是什么,打印出来?该API函数的说明详见:https://www.cctry.com/thread-298708-1-1.html
3、调用 GetDiskFreeSpace 获得系统盘的总容量,已用容量,剩余容量,该API函数的说明详见:https://www.cctry.com/thread-298709-1-1.html
4、调用 GetDiskFreeSpaceEx 获得系统盘的总容量,已用容量,剩余容量,该API函数的说明详见:https://www.cctry.com/thread-298710-1-1.html

#include <Windows.h>
#include <tchar.h>
#include <bitset>
#include <iostream>
using namespace std;
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, INT nCmdShow)
{

    return 0;
}
bool CheckDiskExist(LPCTSTR lpszDrive)
{
    if (!lpszDrive || _tcslen(lpszDrive) < 3) return FALSE;
    TCHAR szDrives[MAX_PATH] = {0};
    DWORD dwRet = GetLogicalDriveStrings(MAX_PATH, szDrives);
    if (!dwRet) return FALSE;
    TCHAR* pDrive = szDrives;
    do
    {
        if (*pDrive) if (_tcscmp(lpszDrive, pDrive) == 0) return true;
        pDrive += _tcslen(pDrive) + 1;
    } while (*pDrive);
}
bool EnumDrives2()
{
    char chDrive = A;
    DWORD dwDrives = GetLogicalDrives();
                            //循环次数28次            
    for (int idx = 0; idx < sizeof(DWORD) * 7; ++idx, ++chDrive)
    {
        DWORD dwValue = dwDrives >> idx; //右移1位相当于除以2,这个只有正数适用,而负数不适用。
        if((dwValue & 1) > 0) cout << chDrive << ":\\" << endl;
        //按位与二进位均为1时, 结果位才为1, 否则为0。
    }
    return TRUE;
}
bool GetDiskSpace(LPCTSTR lpDisk)
{
    if (!lpDisk || _tcslen(lpDisk) < 3) return FALSE;
    DWORD dwSectorsPerCluster = 0;    //每个簇有多少个扇区
    DWORD dwBytesPerSector = 0;        //每个扇区有多少个字节
    DWORD dwNumberOfFreeClusters = 0;    //空余簇的数量
    DWORD dwTotalNumberOfClusters = 0;//全部簇的数量
    bool bRet = GetDiskFreeSpace(lpDisk,&dwSectorsPerCluster,&dwBytesPerSector,&dwNumberOfFreeClusters,&dwTotalNumberOfClusters);
    if (!bRet) return FALSE;
    cout << "使用 GetDiskFreeSpace 函数获取磁盘空间信息" << endl;
    cout << "每簇的扇区数 = " << dwSectorsPerCluster << endl;
    cout << "每扇区的字节数 = " << dwBytesPerSector << endl;
    cout << "空余簇的数量 = " << (DWORD64)dwNumberOfFreeClusters * (DWORD64)dwSectorsPerCluster * (DWORD64)dwBytesPerSector << endl;
    cout << "磁盘总容量 = " << (DWORD64)dwTotalNumberOfClusters * (DWORD64)dwSectorsPerCluster * (DWORD64)dwBytesPerSector << endl;
    //DWORD空间不够存放转为DWORD64
    return TRUE;
}
bool GetDiskSpaceEx(LPCTSTR lpDisk)
{
    if (!lpDisk || _tcslen(lpDisk) < 3) return FALSE;
    DWORD64 qwFreeBytesAvailableToCaller =  0 ;
    DWORD64 qwTotalNumberOfBytes =  0 ;
    DWORD64 qwTotalNumberOfFreeBytes =  0 ;
    bool bRet = GetDiskFreeSpaceEx(lpDisk, (PULARGE_INTEGER)&qwFreeBytesAvailableToCaller, (PULARGE_INTEGER)&qwTotalNumberOfBytes, (PULARGE_INTEGER)&qwTotalNumberOfFreeBytes);
    if (!bRet) return FALSE;
    cout << "使用 GetDiskFreeSpaceEx 函数获取磁盘空间信息" << endl;
    cout << "可用的磁盘空闲容量: " << qwFreeBytesAvailableToCaller << endl;
    cout << "磁盘总容量: " << qwTotalNumberOfBytes << endl;
    cout << "磁盘空闲容量: " << qwTotalNumberOfFreeBytes << endl;
    return TRUE;
}
int main()
{
    //CheckDiskExist(_T("D:\\"));
    //EnumDrives2();
    //GetDiskSpace(_T("D:\\"));
    GetDiskSpaceEx(_T("D:\\"));
    return 0;
}

 

获取磁盘分区信息

标签:sizeof   byte   amp   www   line   打印   log   col   winapi   

原文地址:https://www.cnblogs.com/SakuraQAQ/p/14320361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!