GetLogicalDrives加上c++的附加信息

GetLogicalDrives plus additional information C++

本文关键字:信息 加上 c++ GetLogicalDrives      更新时间:2023-10-16

我的一个程序需要帮助,它可以在系统上取出可用的驱动器并打印有关驱动器的各种信息。我正在使用vc++,我对c++相当陌生,需要一些高级输入或经验丰富的程序员的示例代码。

这是我当前的源代码:

#include "stdafx.h"
#include Windows.h 
#include stdio.h 
#include iostream 
using namespace std; 
int main()
{
    // Initial Dummy drive
    WCHAR myDrives[] = L" A";  
    // Get the logical drive bitmask (1st drive at bit position 0, 2nd drive at bit position 1... so on)  
    DWORD myDrivesBitMask = GetLogicalDrives();  
    // Verifying the returned drive mask  
    if(myDrivesBitMask == 0)   
        wprintf(L"GetLogicalDrives() failed with error code: %dn", GetLastError());  
    else  { 
        wprintf(L"This machine has the following logical drives:n");   
        while(myDrivesBitMask)     {      
            // Use the bitwise AND with 1 to identify 
            // whether there is a drive present or not. 
            if(myDrivesBitMask & 1)    {     
                // Printing out the available drives     
                wprintf(L"drive %sn", myDrives);    
            }    
            // increment counter for the next available drive.   
            myDrives[1]++;    
            // shift the bitmask binary right    
            myDrivesBitMask >>= 1;   
        }   
        wprintf(L"n");  
    }  
    system("pause");   
}

'

This machine has the following logical drives:
drive  C
drive  D
drive  E
drive  F
drive  G
drive  H
drive  I

我需要输出关于每个驱动器的额外信息(也许一个示例将在更短的时间内说明这个故事):

Drive - C:驱动类型:固定Drive Ready Status: True卷标:引导驱动器文件系统类型:NTFS空闲空间:30021926912总驱动器大小:240055742464

Drive - D:驱动类型:固定Drive Ready Status: True卷标:应用数据文件系统类型:NTFS空闲空间:42462507008总驱动器大小:240054693888

我可以使用哪些方法,libs api等来拔出驱动器类型,驱动器状态,卷标签,文件系统类型,可用空间和总驱动器大小?

*旁注,我注意到我的预处理器指令有一个缺陷,特别是在标准I/O头文件中。我知道这不是使用printf的推荐方式cout是类型安全的,也是正确的路径但是我不知道如何在cout中格式化输出,就像在wprintf(L"drive %sn", myDrives);.... so how would you do this with cout??

中那样

提前感谢。

您需要查看GetVolumeInformation等函数来检索文件系统信息,如可用空间和卷名。

GetDriveType将为您提供有关驱动器类型的一些基本信息,但USB拇指棒和闪存读取器可以给出令人惊讶的结果。

我不确定你所说的"就绪状态"是什么意思。如果您指的是驱动器中是否存在有效卷,那么您可以尝试CreateFile的路径为"\"。C:"试着打开音量。如果失败,则没有卷(磁盘)存在。这将用于SD卡读卡器。要做到这一点而不出现错误对话框,您需要先调用SetErrorMode(SEM_NOOPENFILEERRORBOX)

要检查驱动器是否准备就绪,您也可以使用GetDiskFreeSpaceEx。如果这失败,驱动器还没有准备好/可用。

下面是一些示例代码:http://pinvoke.net/default.aspx/coredll/GetDiskFreeSpaceEx.html