【问题标题】:How do I detect when a removable disk is inserted using C#?如何检测何时使用 C# 插入可移动磁盘?
【发布时间】:2008-11-07 04:24:05
【问题描述】:

我只关心 Windows,所以没有必要深究 Mono 兼容性或类似的事情。

我还应该补充一点,我正在编写的应用程序是 WPF,如果可能的话,我宁愿避免依赖 System.Windows.Forms

【问题讨论】:

  • 我们是在谈论 USB 端口吗?
  • USB 驱动器是可移动磁盘的一个示例,但 Windows 在处理事件时通常将它们视为光驱等。

标签: c# wpf windows


【解决方案1】:

试一试...

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace WMITestConsolApplication
{

    class Program
    {

        static void Main(string[] args)
        {

            AddInsertUSBHandler();
            AddRemoveUSBHandler();
            while (true) {
            }

        }

        static ManagementEventWatcher w = null;

        static void AddRemoveUSBHandler()
        {

            WqlEventQuery q;
            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            try {

                q = new WqlEventQuery();
                q.EventClassName = "__InstanceDeletionEvent";
                q.WithinInterval = new TimeSpan(0, 0, 3);
                q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += USBRemoved;

                w.Start();
            }
            catch (Exception e) {


                Console.WriteLine(e.Message);
                if (w != null)
                {
                    w.Stop();

                }
            }

        }

        static void AddInsertUSBHandler()
        {

            WqlEventQuery q;
            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            try {

                q = new WqlEventQuery();
                q.EventClassName = "__InstanceCreationEvent";
                q.WithinInterval = new TimeSpan(0, 0, 3);
                q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += USBInserted;

                w.Start();
            }
            catch (Exception e) {

                Console.WriteLine(e.Message);
                if (w != null)
                {
                    w.Stop();

                }
            }

        }

        static void USBInserted(object sender, EventArgs e)
        {

            Console.WriteLine("A USB device inserted");

        }

        static void USBRemoved(object sender, EventArgs e)
        {

            Console.WriteLine("A USB device removed");

        }
    }

}

【讨论】:

  • ManagementEventWatcher 在哪个程序集中?
  • System.Management,根据这篇 MSDN 文章:msdn.microsoft.com/en-us/library/…
  • 看起来它在 System.Management.dll - msdn.microsoft.com/en-us/library/…
  • 在我的系统上,当我插入时,我得到 4 个插入和 4 个删除事件,然后拔出我的笔式驱动器......
  • 在我的系统上,我得到两个插入事件和两个删除事件。好像分不清他们。有什么想法吗?
【解决方案2】:

执行此操作的方法比使用 WMI 轮询要简单得多 - 只需捕获 WM_DEVICECHANGE:

http://msdn.microsoft.com/en-us/library/aa363215.aspx

【讨论】:

    【解决方案3】:

    最简单的方法是创建一个自动播放处理程序:

    http://www.codeproject.com/KB/system/AutoplayDemo.aspx

    自动播放版本 2 是 将首先扫描的 Windows XP 四个级别的可移动媒体,当 它到了,正在寻找媒体内容 类型(音乐、图形或视频)。 申请登记已完成 基于内容类型。当一个 可移动媒体到货,Windows XP 确定要执行的操作 评估内容和比较 它向注册的处理程序 内容。

    detailed MSDN article 也可用。

    【讨论】:

    • 这很酷,但我真的只是在寻找可以在我的软件运行时运行的东西。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 2012-01-23
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多