類代碼:
/// <summary>
/// 聲明一個委托,用于代理一系列自定義方法
/// </summary>
public delegate void FlashDiskDelegate(string[] ReturnValue);
/// <summary>
/// U盤檢測
/// </summary>
internal class FlashDisk
{
/// <summary>
/// 聲明一個綁定于上行所定義的委托的事件
/// </summary>
public event FlashDiskDelegate FlashDiskEvent;
private const int WmDeviceChange = 0x219;//U盤插入後,OS的底層會自動檢測到,然後向應用程式發送“硬體裝置狀态改變“的消息
private const int DbtDeviceArrival = 0x8000; //就是用來表示U盤可用的。一個裝置或媒體已被插入一塊,現在可用。
private const int DbtConfigChangeCanceled = 0x0019; //要求更改目前的配置(或取消停靠碼頭)已被取消。
private const int DbtConfigchanged = 0x0018; //目前的配置發生了變化,由于碼頭或取消固定。
private const int DbtCustomEvent = 0x8006; //自定義的事件發生。 的Windows NT 4.0和Windows 95:此值不支援。
private const int DbtDeviceQueryRemove = 0x8001; //審批要求删除一個裝置或媒體作品。任何應用程式也不能否認這一要求,并取消删除。
private const int DbtDeviceQueryRemoveFailed = 0x8002; //請求删除一個裝置或媒體片已被取消。
private const int DbtDeviceRemoveComplete = 0x8004; //一個裝置或媒體片已被删除。
private const int DbtDeviceRemovePending = 0x8003; //一個裝置或媒體一塊即将被删除。不能否認的。
private const int DbtDeviceTypeSpecific = 0x8005; //一個裝置特定事件發生。
private const int DbtDevNodesChanged = 0x0007; //一種裝置已被添加到或從系統中删除。
private const int DbtQueryChangeConfig = 0x0017; //許可是要求改變目前的配置(碼頭或取消固定)。
private const int DbtUserDefined = 0xFFFF; //此消息的含義是使用者定義的
public string[] GetRemovableDrivers(int msg, IntPtr wParam)
{
try
{
if (msg != WmDeviceChange)
{
return null;
}
switch (wParam.ToInt32())
{
case WmDeviceChange:
break;
case DbtDeviceArrival://檢測到U盤插入
{
var driveInfos = DriveInfo.GetDrives();
var flashDisks = from driveInfo in driveInfos
where driveInfo.DriveType == DriveType.Removable
select driveInfo.Name;
return flashDisks.ToArray();
}
case DbtDeviceRemoveComplete: //檢測到U盤拔出
{
Trace.Write("U盤拔出");
var driveInfos = DriveInfo.GetDrives();
var flashDisks = from driveInfo in driveInfos
where driveInfo.DriveType == DriveType.Removable
select driveInfo.Name;
if (FlashDiskEvent != null)
{
FlashDiskEvent(flashDisks.ToArray());//先隐藏圖示,在判定是否還有U盤存在
}
return flashDisks.ToArray();
}
case DbtConfigChangeCanceled:
break;
case DbtConfigchanged:
break;
case DbtCustomEvent:
break;
case DbtDeviceQueryRemove:
break;
case DbtDeviceQueryRemoveFailed:
break;
case DbtDeviceRemovePending:
break;
case DbtDeviceTypeSpecific:
break;
case DbtQueryChangeConfig:
break;
case DbtUserDefined:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
}
調用:
FlashDisk flashDisk = new FlashDisk();//定義類
#region 窗體初始化
public MainWindow()
{
InitializeComponent();
flashDisk.FlashDiskEvent += FlashDiskShow;
}
#endregion
#region 窗體加載
private void Window_Loaded(object sender, RoutedEventArgs e)
{
/*WPF中處理消息首先要擷取視窗句柄,建立HwndSource對象 通過HwndSource對象添加
* 消息處理回調函數.HwndSource類: 實作其自己的視窗過程。 建立視窗之後使用 AddHook
* 和 RemoveHook 來添加和移除挂鈎,接收所有視窗消息。*/
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;//視窗過程
if (hwndSource != null)
{
hwndSource.AddHook(new HwndSourceHook(WndProc));//挂鈎
}
}
#endregion
#region 事件 U盤監聽
private void FlashDiskShow(string[] ReturnValue)
{
lblUDick.Visibility = Visibility.Hidden;
lblUDick.Content = "";
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
try
{
var aa = flashDisk.GetRemovableDrivers(msg, wParam);
if (aa == null || aa.Length < 1)
{
return IntPtr.Zero;
}
var ds = string.Empty;
for (int i = 0; i < aa.Length; i++)
{
ds += aa[i] + " ";//aa[i] + Environment.NewLine;
}
lblUDick.Visibility = Visibility.Visible;
lblUDick.Content = "U盤(" + ds + ")" + "已連接配接";
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return IntPtr.Zero;
}
#endregion