有同學向我問這個問題,於是就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;
這個結構體僅僅是一個“頭”(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盤則都等於0
所以,我只需要在程序中捕捉
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);
}
}
}