曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4359|回复: 0
打印 上一主题 下一主题

[文件与IO] GetDriveType() -- 获取驱动器类型

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2012-2-13 18:35:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 22:59 , Processed in 0.083980 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表