在c#中检测驱动器挂载事件
本文关键字:事件 驱动器 检测 | 更新日期: 2023-09-27 18:15:13
如何捕获一个事件,当一个新的驱动器被添加到我的计算机,最好,当一些驱动器的新挂载点被创建在NTFS驱动器上?
我找到了这个,但是它不能在挂载的文件夹上工作:
_eventWatcher = new ManagementEventWatcher("SELECT * FROM Win32_VolumeChangeEvent");
_eventWatcher.EventArrived += (o, args) =>
{switch(args.NewEvent["EventType"].ToString()[0])
{
case '2':
//mount
Debug.WriteLine(args.NewEvent["DriveName"]);
break;
case '3':
//unmount
break;
}
};
_eventWatcher.Start();
任何想法?
如果你有一个表单,你可以重写它的WndProc方法来捕获WM_DEVICECHANGE消息,正如Eugene提到的:
private const int WM_DEVICECHANGE = 0x219;
protected override void WndProc(ref Message m)
{
base.WndProc(m);
if (m.Msg == WM_DEVICECHANGE)
{
// Check m.wParam to see exactly what happened
}
}