检测U盘代码 C#代码
public partial class Form1 : Form
{
public const int WM_DEVICECHANGE = 0x219;
public const int DBT_DEVICEARRIVAL = 0x8000;
public const int DBT_CONFIGCHANGECANCELED = 0x0019;
public const int DBT_CONFIGCHANGED = 0x0018;
public const int DBT_CUSTOMEVENT = 0x8006;
public const int DBT_DEVICEQUERYREMOVE = 0x8001;
public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int DBT_DEVICEREMOVEPENDING = 0x8003;
public const int DBT_DEVICETYPESPECIFIC = 0x8005;
public const int DBT_DEVNODES_CHANGED = 0x0007;
public const int DBT_QUERYCHANGECONFIG = 0x0017;
public const int DBT_USERDEFINED = 0xFFFF;
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE)
{
StringBuilder str = new StringBuilder();
switch (m.WParam.ToInt32())
{
case DBT_DEVNODES_CHANGED:
DriveInfo[] drivers = DriveInfo.GetDrives();
foreach (DriveInfo info in drivers)
{
if (info.DriveType == DriveType.Removable)
{
str.Append("盘符:" + info.Name);
}
}
MessageBox.Show(str.ToString());
break;
}
}
base.WndProc(ref m);
}
}
|