using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices
;
namespace WindowsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_VOLUME
{
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
}
protected override void WndProc(ref Message m)
{
const int WM_DEVICECHANGE = 0x0219;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVECOMPLETE = 0x8001;
const int DBT_DEVTYP_VOLUME = 0x00000002;
switch (m.Msg)
{
case WM_DEVICECHANGE:
switch (m.WParam.ToInt32())
{
case DBT_DEVICEARRIVAL:
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(
m.LParam, typeof(DEV_BROADCAST_VOLUME));
MessageBox.Show(vol.dbcv_unitmask.ToString("x"));
}
break;
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show("Removal");
break;
}
break;
}
base.WndProc(ref m);
}
}
} 复制代码
下边资料是关于C#监控是否有u盘插入的代码,希望能对小伙伴有所用处。
转载于:https://juejin.im/post/5c5158cde51d457ecb1172be