如何在将新驱动器添加到“我的电脑”时捕获事件,并且最好在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();
有什么想法吗?
答案 0 :(得分:7)
如果您有表单,可以覆盖其WndProc方法以捕获Eugene提到的WM_DEVICECHANGE消息:
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
}
}