0
嗨我必須開發一個應用程序,使用C#檢測USB硬盤。可能嗎?。我有一個用於檢測USB大容量存儲設備的代碼。 我添加了對System.Runtime.InteropServices的引用 我試圖覆蓋WndProc()方法。我將包括片段如何使用c編程檢測USB硬盤#
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if ((message.Msg != WM_DEVICECHANGE) || (message.LParam == IntPtr.Zero))
return;
DEV_BROADCAST_VOLUME volume = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(message.LParam, typeof(DEV_BROADCAST_VOLUME));
if (volume.dbcv_devicetype == DBT_DEVTYP_VOLUME)
{
switch (message.WParam.ToInt32())
{
// New device inserted...
case DBT_DEVICEARRIVAL:
MessageBox.Show(
string.Format("A storage device has been inserted; Drive :{0}", ToDriveName(volume.dbcv_unitmask)), "Detect USB");
break;
// Device Removed.
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show("Storage has been removed.", "Detect USB");
break;
}
}
}
}
此鏈接幫助我:http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html – OhMyGeo