1.获取所有的驱动器
利用函数
GetLogicalDriveStrings
The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.
DWORD GetLogicalDriveStrings(
DWORD nBufferLength, // size of buffer
LPTSTR lpBuffer // drive strings buffer
);
很简单的一个函数,msdn有详细的说明
需要注意的一点是
lpBuffer中最后获得的数据是这样c:/<null>d:/<null><null>,每两个路径之间都间隔一个 null-terminated,
所以,如果你直接cout<<lpBuffer 的话,那么得到的是C:/,很是令人郁闷,于是要想办法把这些路径一个一个取得
所以,有了如下代码
TCHAR szBuf[100];
memset(szBuf,0,100);
DWORD len = GetLogicalDriveStrings(sizeof(szBuf)/sizeof(TCHAR),szBuf);
for (TCHAR* s = szBuf; *s; s += _tcslen(s)+1)
{
LPCTSTR sDrivePath = s;
cout<<sDrivePath
}
那么这个sDrivePath 就是一个一个的类似于C:/,D:/那样的字符窜
2.获取驱动器类型
现在获得了驱动器的路径了,如C:/,D:/
那么如何区分他们呢,
有这个函数
GetDriveType
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
UINT GetDriveType(
LPCTSTR lpRootPathName // root directory
);
UINT uDriveType = GetDriveType(sDrivePath);
调用以后,这个函数的返回值有
Value Meaning
DRIVE_UNKNOWN The drive type cannot be determined.
DRIVE_NO_ROOT_DIR The root path is invalid. For example, no volume is mounted at the path.
DRIVE_REMOVABLE The disk can be removed from the drive.
DRIVE_FIXED The disk cannot be removed from the drive.
DRIVE_REMOTE The drive is a remote (network) drive.
DRIVE_CDROM The drive is a CD-ROM drive.
DRIVE_RAMDISK The drive is a RAM disk.
但是,靠这个函数,很多东西,都是区分不了了,比如软驱,和插入的U盘,都是DRIVE_REMOVABLE ,而硬盘和插入的移动硬盘,都是DRIVE_FIXED
靠:(
我们一个一个来试试吧
3.获取光驱
先捏软柿子:)
UINT uDriveType = GetDriveType(sDrivePath);
if (uDriveType == DRIVE_CDROM)
{
这个就是咯
}
要注意的是,虽然写的是DRIVE_CDROM
但是dvd 光驱也能获得(这不废话吗),另外,虚拟光驱也能获得,比如俺机器上安装了Alcohol 120%,设置的虚拟光驱也获得了
4.区分软驱和U盘
先把代码贴出来吧
#define MEDIA_INFO_SIZE sizeof(GET_MEDIA_TYPES)+15*sizeof(DEVICE_MEDIA_INFO)
BOOL GetDriveGeometry(const TCHAR * filename, DISK_GEOMETRY * pdg
利用函数
GetLogicalDriveStrings
The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.
DWORD GetLogicalDriveStrings(
DWORD nBufferLength, // size of buffer
LPTSTR lpBuffer // drive strings buffer
);
很简单的一个函数,msdn有详细的说明
需要注意的一点是
lpBuffer中最后获得的数据是这样c:/<null>d:/<null><null>,每两个路径之间都间隔一个 null-terminated,
所以,如果你直接cout<<lpBuffer 的话,那么得到的是C:/,很是令人郁闷,于是要想办法把这些路径一个一个取得
所以,有了如下代码
TCHAR szBuf[100];
memset(szBuf,0,100);
DWORD len = GetLogicalDriveStrings(sizeof(szBuf)/sizeof(TCHAR),szBuf);
for (TCHAR* s = szBuf; *s; s += _tcslen(s)+1)
{
LPCTSTR sDrivePath = s;
cout<<sDrivePath
}
那么这个sDrivePath 就是一个一个的类似于C:/,D:/那样的字符窜
2.获取驱动器类型
现在获得了驱动器的路径了,如C:/,D:/
那么如何区分他们呢,
有这个函数
GetDriveType
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
UINT GetDriveType(
LPCTSTR lpRootPathName // root directory
);
UINT uDriveType = GetDriveType(sDrivePath);
调用以后,这个函数的返回值有
Value Meaning
DRIVE_UNKNOWN The drive type cannot be determined.
DRIVE_NO_ROOT_DIR The root path is invalid. For example, no volume is mounted at the path.
DRIVE_REMOVABLE The disk can be removed from the drive.
DRIVE_FIXED The disk cannot be removed from the drive.
DRIVE_REMOTE The drive is a remote (network) drive.
DRIVE_CDROM The drive is a CD-ROM drive.
DRIVE_RAMDISK The drive is a RAM disk.
但是,靠这个函数,很多东西,都是区分不了了,比如软驱,和插入的U盘,都是DRIVE_REMOVABLE ,而硬盘和插入的移动硬盘,都是DRIVE_FIXED
靠:(
我们一个一个来试试吧
3.获取光驱
先捏软柿子:)
UINT uDriveType = GetDriveType(sDrivePath);
if (uDriveType == DRIVE_CDROM)
{
这个就是咯
}
要注意的是,虽然写的是DRIVE_CDROM
但是dvd 光驱也能获得(这不废话吗),另外,虚拟光驱也能获得,比如俺机器上安装了Alcohol 120%,设置的虚拟光驱也获得了
4.区分软驱和U盘
先把代码贴出来吧
#define MEDIA_INFO_SIZE sizeof(GET_MEDIA_TYPES)+15*sizeof(DEVICE_MEDIA_INFO)
BOOL GetDriveGeometry(const TCHAR * filename, DISK_GEOMETRY * pdg