C++ディスク文字および空き容量の取得

10720 ワード

void test2()
{
    DWORD dwLen = GetLogicalDriveStrings(0, NULL);	//         .
    char * pszDriver = new char[dwLen];				//           .
    GetLogicalDriveStrings(dwLen, pszDriver);		//       .
    vector<string> v;
    while (*pszDriver != '\0')
    {
        cout << pszDriver << " ---- " << endl;
        v.push_back(pszDriver);
        pszDriver += strlen(pszDriver) + 1;			//         .       '\0'   .
    }

    int DType;
    int si = 0;
    BOOL fResult;
    unsigned _int64 i64FreeBytesToCaller;
    unsigned _int64 i64TotalBytes;
    unsigned _int64 i64FreeBytes;

    for (int i = 0; i < dwLen / 4; ++i)
        //            ,         ,  DStr        A:\NULLB:\NULLC:\NULL,     ,  DSLength/4           
    {
        DType = GetDriveType(v[i].c_str());
        //GetDriveType  ,         ,          
        if (DType == DRIVE_FIXED)
        {
            cout << "  ";
        }
        else if (DType == DRIVE_CDROM)
        {
            cout << "  ";
        }
        else if (DType == DRIVE_REMOVABLE)
        {
            cout << "      ";
        }
        else if (DType == DRIVE_REMOTE)
        {
            cout << "    ";
        }
        else if (DType == DRIVE_RAMDISK)
        {
            cout << "  RAM  ";
        }
        else if (DType == DRIVE_UNKNOWN)
        {
            cout << "    ";
        }

        fResult = GetDiskFreeSpaceEx(
            v[i].c_str(),
            (PULARGE_INTEGER)&i64FreeBytesToCaller,
            (PULARGE_INTEGER)&i64TotalBytes,
            (PULARGE_INTEGER)&i64FreeBytes);
        //GetDiskFreeSpaceEx  ,              ,       BOOL    
        if (fResult)//     BOOL              
        {
            cout << " totalspace:" << (float)i64TotalBytes / 1024 / 1024 << " MB";//     
            cout << " freespace:" << (float)i64FreeBytesToCaller / 1024 / 1024 << " MB";//      
        }
        else
        {
            cout << "       ";
        }
        cout << endl;
        si += 4;
    }
}