在.NET中探测U盘的插入/拔出
有同学向我问这个问题,于是就Google了一下找到答案,不过是C下的,我将其改编成了C#的。
当设备被插入/拔出的时候,WINDOWS会向每个窗体发送WM_DEVICECHANGE 消息,当消息的wParam 值等于 DBT_DEVICEARRIVAL 时,表示Media设备被插入并且已经可用;如果wParam值等于DBT_DEVICEREMOVECOMPLETE,表示Media设备已经被移出。
它们的lParam都指向一个 DEV_BROADCAST_HDR结构体,其原形如下:
1 typedef struct _DEV_BROADCAST_HDR
2 {
3 DWORD dbch_size;
4 DWORD dbch_devicetype;
5 DWORD dbch_reserved;
6 } DEV_BROADCAST_HDR, *PDEV_BROADCAST_HDR;
2 {
3 DWORD dbch_size;
4 DWORD dbch_devicetype;
5 DWORD dbch_reserved;
6 } DEV_BROADCAST_HDR, *PDEV_BROADCAST_HDR;
这个结构体仅仅是一个“头”(HDR),其后还有附加数据,dbch_size表示结构体实例的字节数,当其中的dbch_devicetype字段值等于DBT_DEVTYP_VOLUME时,表示当前设备是逻辑驱动器,且lParam实际上指向的应该是DEV_BROADCAST_VOLUME 结构体实例(真佩服这种逻辑),DEV_BROADCAST_VOLUME 结构体原形如下:
1 typedef struct _DEV_BROADCAST_VOLUME {
2 DWORD dbcv_size;
3 DWORD dbcv_devicetype;
4 DWORD dbcv_reserved;
5 DWORD dbcv_unitmask;
6 WORD dbcv_flags;
7 } DEV_BROADCAST_VOLUME, *PDEV_BROADCAST_VOLUME;
其中dbcv_unitmask 字段表示当前改变的驱动器掩码,第一位表示驱动器号A,第二位表示驱动器号B,第三位表示驱动器号C,以此类推…… dbcv_flags 表示驱动器的类别,如果等于1,则是光盘驱动器;如果是2,则是网络驱动器;如果是硬盘、U盘则都等于02 DWORD dbcv_size;
3 DWORD dbcv_devicetype;
4 DWORD dbcv_reserved;
5 DWORD dbcv_unitmask;
6 WORD dbcv_flags;
7 } DEV_BROADCAST_VOLUME, *PDEV_BROADCAST_VOLUME;
所以,我只需要在程序中捕捉WM_DEVICECHANGE 消息,然后根据具体情况去处理即可,下面是我的测试代码:
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UDiskDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
public UInt32 dbch_size;
public UInt32 dbch_devicetype;
public UInt32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_VOLUME
{
public UInt32 dbcv_size;
public UInt32 dbcv_devicetype;
public UInt32 dbcv_reserved;
public UInt32 dbcv_unitmask;
public UInt16 dbcv_flags;
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x0219)//WM_DEVICECHANGE
{
switch (m.WParam.ToInt32())
{
case 0x8000://DBT_DEVICEARRIVAL
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
AddVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
case 0x8004://DBT_DEVICEREMOVECOMPLETE
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
RemoveVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
}
}
base.DefWndProc(ref m);
}
/// <summary>
/// 根据驱动器掩码返回驱动器号数组
/// </summary>
/// <param name="Mask">掩码</param>
/// <returns>返回驱动器号数组</returns>
public static char[] GetVolumes(UInt32 Mask)
{
List<char> Volumes = new List<char>();
for (int i = 0; i < 32; i++)
{
uint p = (uint)Math.Pow(2, i);
if ((p | Mask) == p)
{
Volumes.Add((char)('A' + i));
}
}
return Volumes.ToArray();
}
public void AddVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Add(volume);
}
public void RemoveVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Remove(volume);
}
}
}
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UDiskDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
public UInt32 dbch_size;
public UInt32 dbch_devicetype;
public UInt32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_VOLUME
{
public UInt32 dbcv_size;
public UInt32 dbcv_devicetype;
public UInt32 dbcv_reserved;
public UInt32 dbcv_unitmask;
public UInt16 dbcv_flags;
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x0219)//WM_DEVICECHANGE
{
switch (m.WParam.ToInt32())
{
case 0x8000://DBT_DEVICEARRIVAL
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
AddVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
case 0x8004://DBT_DEVICEREMOVECOMPLETE
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
RemoveVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
}
}
base.DefWndProc(ref m);
}
/// <summary>
/// 根据驱动器掩码返回驱动器号数组
/// </summary>
/// <param name="Mask">掩码</param>
/// <returns>返回驱动器号数组</returns>
public static char[] GetVolumes(UInt32 Mask)
{
List<char> Volumes = new List<char>();
for (int i = 0; i < 32; i++)
{
uint p = (uint)Math.Pow(2, i);
if ((p | Mask) == p)
{
Volumes.Add((char)('A' + i));
}
}
return Volumes.ToArray();
}
public void AddVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Add(volume);
}
public void RemoveVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Remove(volume);
}
}
}