如何使用 C# 检测何时插入可移动磁盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/271238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:57:15  来源:igfitidea点击:

How do I detect when a removable disk is inserted using C#?

c#wpfwindows

提问by David Mitchell

I'm just concerned about Windows, so there's no need to go into esoterica about Mono compatibility or anything like that.

我只关心 Windows,所以没有必要深入了解 Mono 兼容性或类似的东西。

I should also add that the app that I'm writing is WPF, and I'd prefer to avoid taking a dependency on System.Windows.Formsif at all possible.

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

采纳答案by Josh Stodola

Give this a shot...

试一试这个...

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");

        }
    }

}

回答by ine

The simplest way would be to create an Autoplay Handler:

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

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

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

Autoplay Version 2 is a feature in Windows XP that will scan the first four levels of a removable media, when it arrives, looking for media content types (music, graphics, or video). Registration of applications is done on a content type basis. When a removable media arrives, Windows XP determines what actions to perform by evaluating the content and comparing it to registered handlers for that content.

自动播放版本 2 是 Windows XP 中的一项功能,它将扫描可移动媒体的前四个级别,当它到达时,寻找媒体内容类型(音乐、图形或视频)。应用程序的注册是基于内容类型进行的。当可移动媒体到达时,Windows XP 通过评估内容并将其与该内容的注册处理程序进行比较来确定要执行的操作。

A detailed MSDN articleis also available.

一个详细的MSDN文章也可用。

回答by Ana Betts

There are much less cumbersome ways of doing this than using WMI polling - just capture WM_DEVICECHANGE:

这样做比使用 WMI 轮询要简单得多 - 只需捕获 WM_DEVICECHANGE:

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

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