如何检测媒体是否已插入可移动驱动器/读卡器
我有一个读卡器(未插入记忆棒)。
当我插入计算机时,它在“我的电脑”中显示一个空驱动器...
是否可以知道驱动器是否有媒体(抱歉,我不知道如何称呼它)...
I have a card reader ( no memory stick is inserted ).
When i insert into my compter it shows an empty drive in My Computer...
Is it possible to know whether a drive is having a media ( sorry i am not sure how to call it ) or not...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现 MSalters 使用
IOCTL_STORAGE_CHECK_VERIFY
的建议非常好。IOCTL_STORAGE_CHECK_VERIFY
的使用有一个小技巧。在使用DeviceIoControl
函数中的IOCTL代码之前,需要在CreateFile
函数中打开相应的设备:对于
DeviceIoControl
的使用一可以使用0作为CreateFile
的第二个参数,因为我们不会使用ReadFile
、WriteFile
等函数来访问设备。IOCTL_STORAGE_CHECK_VERIFY
的实现确实遵循一些数据读取请求。因此,为了能够使用IOCTL_STORAGE_CHECK_VERIFY
而不会出现ERROR_ACCESS_DENIED
(5) 错误,我们必须按如下方式打开设备存在另一个版本的
IOCTL_STORAGE_CHECK_VERIFY
-IOCTL_STORAGE_CHECK_VERIFY2
其工作方式与IOCTL_STORAGE_CHECK_VERIFY
完全相同,但速度更快(请参阅 http://msdn.microsoft.com/en-us/library/ff560538.aspx)。要使用IOCTL_STORAGE_CHECK_VERIFY2
,只需使用FILE_READ_ATTRIBUTES
访问权限即可打开设备:测试驱动器中介质是否存在的代码如下所示
I find the suggestion of MSalters to use
IOCTL_STORAGE_CHECK_VERIFY
very good. There are a small trick in the usage ofIOCTL_STORAGE_CHECK_VERIFY
. Before the usage of IOCTL code in the functionDeviceIoControl
one need to open the corresponding device with respect ofCreateFile
function:For the usage of
DeviceIoControl
one can use 0 as a second parameter ofCreateFile
, because we will not useReadFile
,WriteFile
etc functions to access the device. The implementation ofIOCTL_STORAGE_CHECK_VERIFY
do follow to some read of data requests. So to be able to useIOCTL_STORAGE_CHECK_VERIFY
without havingERROR_ACCESS_DENIED
(5) error we have to open the device as followingThere exist another version of
IOCTL_STORAGE_CHECK_VERIFY
-IOCTL_STORAGE_CHECK_VERIFY2
which works absolutely the same asIOCTL_STORAGE_CHECK_VERIFY
but much more quickly (see http://msdn.microsoft.com/en-us/library/ff560538.aspx). To useIOCTL_STORAGE_CHECK_VERIFY2
one can open device with onlyFILE_READ_ATTRIBUTES
access:The code which test the existence of the media in the drive can look like following
这种行为的原因是历史性的,可以追溯到软盘驱动器和 MS-DOS。即使其中没有软盘,
A:
驱动器仍然是A:
驱动器。有时可以检查带有可移动介质的驱动器是否为空。读卡器和 CD 驱动器通常支持此功能,而软盘驱动器则不支持。您可以向驱动器发送
IOCTL_STORAGE_CHECK_VERIFY
。The reason for this behavior is historical, and dates back to floppy drives and MS-DOS. The
A:
drive would still be theA:
drive even if there was no floppy in it.It is sometimes possible to check whether a drive with removable media is empty. Card readers and CD drives usually support this, floppy drives don't. You would send the drive a
IOCTL_STORAGE_CHECK_VERIFY
.