检测驱动器安装在C#中的事件 [英] Detect drive mount event in c#

查看:196
本文介绍了检测驱动器安装在C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕捉一个事件时,一个新的驱动器将被添加到我的电脑,最好是和新时挂载点对于一些驱动器在NTFS硬盘创建的?

How to catch an event when a new drive is added to My Computer and preferably and when new mount point for some drive is created on a NTFS drive?

我figued了这一点,但它不工作在已安装的文件夹:

I figued out this but it doesn't work on mounted folders:

 _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();



任何想法?

Any ideas?

推荐答案

如果你有一个表单,您可以覆盖其WndProc方法捕捉WM_DEVICECHANGE消息作为尤金提到的:

If you have a form, you can override its WndProc method to catch WM_DEVICECHANGE messages as Eugene mentioned:

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
    }
}

这篇关于检测驱动器安装在C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆