如何检测cdrom / usb设备插入? [英] How can I detect cdrom/usb device insertions?

查看:66
本文介绍了如何检测cdrom / usb设备插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#应用程序,需要定期轮询cdroms和usb

存储设备插入。我已经查看了WMI功能,但是没有发现任何有用的东西。最接近的是Win32_DiskDrive,但它似乎没有在cdrom设备上返回任何信息。我怀疑

可能是一个Win32 API调用,但我还没有找到任何使用谷歌的信息。

有没有办法在C#中实现这个目标?

I am writing a C# app that needs to periodically poll for cdroms and usb
storage device insertions. I''ve looked at the WMI functions but haven''t
found anything all that useful. The closest is Win32_DiskDrive, but it
doesn''t seem to return any information on cdrom devices. I suspect there
might be a Win32 API call, but I haven''t found any info yet using Google. Is
there a way to achieve this in C#?

推荐答案

Paul,


查看此CodeProject文章:
http://www.codeproject.com/dotnet/de...umemonitor.asp

Alexander


" ;保罗斯蒂尔 < PA ********* @ acadiau.ca>在消息中写道

news:uA ************** @ TK2MSFTNGP12.phx.gbl ...
Paul,

Have a look at this CodeProject article:
http://www.codeproject.com/dotnet/de...umemonitor.asp

Alexander

"Paul Steele" <pa*********@acadiau.ca> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
我正在写一篇C#需要定期轮询cdroms和usb的应用程序
存储设备插入。我已经查看了WMI函数,但没有找到任何有用的东西。最接近的是Win32_DiskDrive,但它似乎无法返回有关cdrom设备的任何信息。我怀疑
可能是Win32 API调用,但我还没有找到任何使用Google的信息。
有没有办法在C#中实现这一点?
I am writing a C# app that needs to periodically poll for cdroms and usb
storage device insertions. I''ve looked at the WMI functions but haven''t
found anything all that useful. The closest is Win32_DiskDrive, but it
doesn''t seem to return any information on cdrom devices. I suspect there
might be a Win32 API call, but I haven''t found any info yet using Google.
Is there a way to achieve this in C#?





我不确定你是否可以用C#来做,即你有任何直接的课程
$ b这是$ b,但你有用于检测cdroms插入的Win32 API。

以下数据可能对您有所帮助。礼貌,MSDN

检测媒体插入或删除

Windows向所有顶级窗口发送一组默认的WM_DEVICECHANGE

消息添加了新设备或媒体(如CD或DVD),并且当现有设备或媒体被删除时,它们将变为可用的b $ b。您无需

注册即可接收这些默认消息。有关默认发送哪些消息的详细信息,请参阅

RegisterDeviceNotification中的备注部分。

以下代码示例中的消息属于默认消息。


每个WM_DEVICECHANGE消息都有一个相关的事件,描述了

的变化,以及一个提供有关变化的详细信息的结构。

结构由一个与事件无关的头,DEV_BROADCAST_HDR,

后跟事件相关的成员。依赖事件的成员描述事件适用的

设备。要使用此结构,应用程序必须首先确定事件类型和设备类型。然后,他们可以使用

正确的结构来采取适当的行动。

当用户将新的CD或DVD插入驱动器时,应用程序会收到一个

带有DBT_DEVICEARRIVAL事件的WM_DEVICECHANGE消息。应用程序必须检查事件以确保到达的设备类型是一个卷(

dbch_devicetype成员是DBT_DEVTYP_VOLUME)并且该更改会影响

media(dbcv_flags成员是DBTF_MEDIA)。


当用户从驱动器中删除CD或DVD时,应用程序会收到

WM_DEVICECHANGE消息使用DBT_DEVICEREMOVECOMPLETE事件。同样,

应用程序必须检查事件,以确保被删除的设备是一个

的卷,并且该更改会影响媒体。


以下代码演示了如何检查插入或删除

CD或DVD。

#include< windows.h>

#include< dbt.h>


void Main_OnDeviceChange(HWND hwnd,WPARAM wParam,LPARAM lParam);

char FirstDriveFromMask(ULONG unitmask) ; //原型


/ * --------------------------------- ---------------------------------

Main_OnDeviceChange(hwnd,wParam,lParam)


描述

处理发送到应用程序的

顶级窗口的WM_DEVICECHANGE消息。

------------------------------------------------- ------------------- * /


void Main_OnDeviceChange(HWND hwnd,WPARAM wParam,LPARAM lParam)

{

PDEV_BROADCAST_HDR lpdb =(PDEV_BROADCAST_HDR)lParam;

char szMsg [80];


switch(wParam)

{

case DBT_DEVICEARRIVAL:

//检查CD或DVD是否已插入驱动器。

if(lpdb - > dbch_devicetype == DBT_DEVTYP_VOLUME)

{

PDEV_BROADCAST_VOLUME lpdbv =(PDEV_BROADCAST_VOLUME)lpdb;


if( lpdbv - > dbcv_flags& DBTF_MEDIA)

{

wsprintf(szMsg," Drive%c:媒体已经到达。\\ n&n& quot;,

FirstDriveFromMask(lpdbv - > dbcv_unitmask));


MessageBox(hwnd,szMsg," WM_DEVICECHANGE",MB_OK);

}

}

休息;


案例DBT_DEVICEREMOVECOMPLETE:

//检查是否从驱动器中删除了CD或DVD。

if(lpdb - > dbch_devicetype == DBT_DEVTYP_VOLUME)

{

PDEV_BROADCAST_VOLUME lpdbv =(PDEV_BROADCAST_VOLUME)lpdb;


if(lpdbv - > dbcv_flags& ; DBTF_MEDIA)

{

wsprintf(szMsg," Drive%c:Media was removed.\ n,

FirstDriveFromMask(lpdbv - > dbcv_unitmask));


MessageBox(hwnd,szMsg," WM_DEVICECHANGE",MB_OK);

}

}

休息;


默认:

/ *

处理其他的WM_DEVICECHANGE通知

设备或原因。
* /

;

}

}


/ * -------------------------------------- ----------------------------

FirstDriveFromMask(unitmask)


描述

从驱动器号的掩码中查找第一个有效的驱动器号。

掩码必须采用格式bit 0 = A,bit 1 = B,bit 3 = C,

等。确定有效的驱动器号当相应位

设置为1时编辑。


返回找到的第一个驱动器号。

--- -------------------------------------------------- --------------- * /


char FirstDriveFromMask(ULONG unitmask)

{

char i;


for(i = 0;我< 26; ++ i)

{

if(unitmask& 0x1)

break;

unitmask = unitmask> ;> 1;

}


返回(i +''A'');

}

Pradeep Kumar.R


" Paul Steele"写道:
Hi,
I''m not sure whether you can do it in C#, i.e you have any direct classes
for this, but you have Win32 API''s for detecting the cdroms insertions. The
following data may be helpful to you. Courtesy, MSDN

Detecting Media Insertion or Removal
Windows sends all top-level windows a set of default WM_DEVICECHANGE
messages when new devices or media (such as a CD or DVD) are added and become
available, and when existing devices or media are removed. You do not need to
register to receive these default messages. See the Remarks section in
RegisterDeviceNotification for details on which messages are sent by default.
The messages in the code example below are among the default messages.

Each WM_DEVICECHANGE message has an associated event that describes the
change, and a structure that provides detailed information about the change.
The structure consists of an event-independent header, DEV_BROADCAST_HDR,
followed by event-dependent members. The event-dependent members describe the
device to which the event applies. To use this structure, applications must
first determine the event type and the device type. Then, they can use the
correct structure to take appropriate action.
When the user inserts a new CD or DVD into a drive, applications receive a
WM_DEVICECHANGE message with a DBT_DEVICEARRIVAL event. The application must
check the event to ensure that the type of device arriving is a volume (the
dbch_devicetype member is DBT_DEVTYP_VOLUME) and that the change affects the
media (the dbcv_flags member is DBTF_MEDIA).

When the user removes a CD or DVD from a drive, applications receive a
WM_DEVICECHANGE message with a DBT_DEVICEREMOVECOMPLETE event. Again, the
application must check the event to ensure that the device being removed is a
volume and that the change affects the media.

The following code demonstrates how to check for insertion or removal of a
CD or DVD.
#include <windows.h>
#include <dbt.h>

void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam);
char FirstDriveFromMask (ULONG unitmask); //prototype

/*------------------------------------------------------------------
Main_OnDeviceChange (hwnd, wParam, lParam)

Description
Handles WM_DEVICECHANGE messages sent to the application''s
top-level window.
--------------------------------------------------------------------*/

void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
char szMsg[80];

switch(wParam)
{
case DBT_DEVICEARRIVAL:
// Check whether a CD or DVD was inserted into a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media has arrived.\n",
FirstDriveFromMask(lpdbv ->dbcv_unitmask));

MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
}
}
break;

case DBT_DEVICEREMOVECOMPLETE:
// Check whether a CD or DVD was removed from a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media was removed.\n",
FirstDriveFromMask(lpdbv ->dbcv_unitmask));

MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
}
}
break;

default:
/*
Process other WM_DEVICECHANGE notifications for other
devices or reasons.
*/
;
}
}

/*------------------------------------------------------------------
FirstDriveFromMask (unitmask)

Description
Finds the first valid drive letter from a mask of drive letters.
The mask must be in the format bit 0 = A, bit 1 = B, bit 3 = C,
etc. A valid drive letter is defined when the corresponding bit
is set to 1.

Returns the first drive letter that was found.
--------------------------------------------------------------------*/

char FirstDriveFromMask (ULONG unitmask)
{
char i;

for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}

return (i + ''A'');
}

Pradeep Kumar.R

"Paul Steele" wrote:
我正在编写一个C#应用程序,需要定期轮询cdroms和usb
存储设备插入。我已经查看了WMI函数,但没有找到任何有用的东西。最接近的是Win32_DiskDrive,但它似乎无法返回有关cdrom设备的任何信息。我怀疑
可能是一个Win32 API调用,但我还没有找到任何使用谷歌的信息。有没有办法在C#中实现这个目标?
I am writing a C# app that needs to periodically poll for cdroms and usb
storage device insertions. I''ve looked at the WMI functions but haven''t
found anything all that useful. The closest is Win32_DiskDrive, but it
doesn''t seem to return any information on cdrom devices. I suspect there
might be a Win32 API call, but I haven''t found any info yet using Google. Is
there a way to achieve this in C#?



这绝对是一个很好的起点。不幸的是它没有检测到

USB密钥插入...


Alexander Shirshov <人******* @ omnitalented.com>在消息中写道

news:ef ************** @ TK2MSFTNGP09.phx.gbl ...
This is definitely a good starting point. Unfortunately it does not detect
USB key insertions...

"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:ef**************@TK2MSFTNGP09.phx.gbl...
Paul,
请查看此CodeProject文章:
http ://www.codeproject.com/dotnet/de...umemonitor.asp

Alexander

Paul Steele < PA ********* @ acadiau.ca>在消息中写道
新闻:uA ************** @ TK2MSFTNGP12.phx.gbl ...
Paul,

Have a look at this CodeProject article:
http://www.codeproject.com/dotnet/de...umemonitor.asp

Alexander

"Paul Steele" <pa*********@acadiau.ca> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
我正在编写一个需要的C#应用​​程序定期轮询cdroms和usb
存储设备插入。我已经查看了WMI函数,但没有找到任何有用的东西。最接近的是Win32_DiskDrive,但它似乎无法返回有关cdrom设备的任何信息。我怀疑
可能是Win32 API调用,但我还没有找到任何使用Google的信息。
有没有办法在C#中实现这一点?
I am writing a C# app that needs to periodically poll for cdroms and usb
storage device insertions. I''ve looked at the WMI functions but haven''t
found anything all that useful. The closest is Win32_DiskDrive, but it
doesn''t seem to return any information on cdrom devices. I suspect there
might be a Win32 API call, but I haven''t found any info yet using Google.
Is there a way to achieve this in C#?




这篇关于如何检测cdrom / usb设备插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆