曲径通幽论坛

标题: GetDriveType() -- 获取驱动器类型 [打印本页]

作者: beyes    时间: 2012-2-13 18:35
标题: GetDriveType() -- 获取驱动器类型
GetDriveType() 函数原型如下:
UINT WINAPI GetDriveType(
  __in_opt  LPCTSTR lpRootPathName
);
该函数获取驱动器类型(如硬盘,可移动存储器,网络存储设备等),它只有一个参数,表示的是驱动器的根路径,如 "C:\" ,这个路径可以用GetLogicalDriveStrings()  函数获得。

测试代码:
[C++] 纯文本查看 复制代码
#include "stdafx.h"

#define MaxLength 124

int _tmain(int argc, _TCHAR* argv[])
{
   
     
    TCHAR buffer[MaxLength];
    UINT DriveType;
     
    GetLogicalDriveStrings(MaxLength, buffer);

    PTSTR Devices = (PTSTR)buffer;

    do {
        _tprintf (TEXT("The Type of %s : "), Devices);
        DriveType = GetDriveType(Devices);
        switch (DriveType) {
        case DRIVE_UNKNOWN:
            _tprintf (TEXT("The drive type cannot be determined."));
            break;
        case DRIVE_NO_ROOT_DIR:
            _tprintf (TEXT("The root path is invalid; for example, there is no volume mounted at the specified path."));
            break;
        case DRIVE_REMOVABLE:
            _tprintf (TEXT("The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader."));
            break;
        case DRIVE_FIXED:
            _tprintf (TEXT("The drive has fixed media; for example, a hard drive or flash drive."));
            break;
        case DRIVE_REMOTE:
            _tprintf (TEXT("The drive is a remote (network) drive."));
            break;
        case DRIVE_CDROM:
            _tprintf (TEXT("The drive is a CD-ROM drive."));
            break;
        case DRIVE_RAMDISK:
            _tprintf (TEXT("The drive is a RAM disk."));
            break;
        default:
            break;
        }
        _tprintf (TEXT("\n"));

        Devices += (lstrlen((LPCWSTR)Devices) + 1);
    }while (*Devices);

   

    return 0;
}

运行输出:
The Type of C:\ : The drive has fixed media; for example, a hard drive or flash drive.
The Type of D:\ : The drive has fixed media; for example, a hard drive or flash drive.
The Type of E:\ : The drive has fixed media; for example, a hard drive or flash drive.
The Type of F:\ : The drive is a CD-ROM drive.
The Type of X:\ : The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
The Type of Z:\ : The drive is a CD-ROM drive.





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2