简体   繁体   English

如何在服务中插入和删除事件中的USB磁盘驱动器,而不会在某些操作系统上反复访问我的硬盘驱动器?

[英]How do I get USB disk drive inserted and removed events in a service without hearing my hard drive being accessed repeatedly on some O/S's?

I use this code in my Windows Service to be notified of USB disk drives being inserted and removed: 我在Windows服务中使用此代码来通知插入和删除USB磁盘驱动器:

WqlEventQuery query = new WqlEventQuery("__InstanceOperationEvent", 
    "TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType=2");
query.WithinInterval = TimeSpan.FromSeconds(1);
_deviceWatcher = new ManagementEventWatcher(query);
_deviceWatcher.EventArrived += new EventArrivedEventHandler(OnDeviceEventArrived);
_deviceWatcher.Start();

It works on XP and Vista, but on XP I can hear the very noticeable sound of the hard drive being accessed every second. 它适用于XP和Vista,但在XP上,我可以听到每秒钟访问硬盘的非常明显的声音。 Is there another WMI query that will give me the events without the sound effect? 是否有另一个WMI查询会给我没有声音效果的事件?

Not sure if this applies to your case but we've been using RegisterDeviceNotification in our C# code (which I can't post here) to detect when USB devices are plugged in. There's a handful of native functions you have to import but it generally works well. 不确定这是否适用于你的情况但是我们一直在我们的C#代码中使用RegisterDeviceNotification(我不能在这里发布)来检测何时插入USB设备。你必须导入一些本机功能,但它通常效果很好。 Easiest to make it work in C++ first and then see what you have to move up into C#. 最容易使它在C ++中工作,然后看看你有什么要升级到C#。

There's some stuff on koders Code search that appears to be a whole C# device management module that might help: koders上的一些东西代码搜索似乎是一个可能有帮助的整个C#设备管理模块:

http://www.koders.com/csharp/fidEF5C6B3E2F46BE9AAFC93DB75515DEFC46DB4101.aspx http://www.koders.com/csharp/fidEF5C6B3E2F46BE9AAFC93DB75515DEFC46DB4101.aspx

Try looking for the InstanceCreationEvent, which will signal the creation of a new Win32_LogicalDisk instance. 尝试查找InstanceCreationEvent,它将指示创建新的Win32_LogicalDisk实例。 Right now you're querying for instance operations, not creations. 现在你正在查询实例操作,而不是创作。 You should know that the query interval on those events is pretty long - it's possible to pop a USB in and out faster that you'll detect. 您应该知道这些事件的查询间隔时间很长 - 您可以更快地弹出USB进入和检出。

try this 试试这个

using System;
using System.Management;

namespace MonitorDrives
{
class Program
{
    public enum EventType
    {
        Inserted = 2,
        Removed = 3
    }

    static void Main(string[] args)
    {
        ManagementEventWatcher watcher = new ManagementEventWatcher();
        WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3");

        watcher.EventArrived += (s, e) =>
        {
            string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
            EventType eventType = (EventType)(Convert.ToInt16(e.NewEvent.Properties["EventType"].Value));

            string eventName = Enum.GetName(typeof(EventType), eventType);

            Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName);
        };

        watcher.Query = query;
        watcher.Start();

        Console.ReadKey();
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获得USB驱动器的名称? - How do you get a USB Drive's Name in C#? 如何在c#中新插入USB驱动器盘符? - How do I get newly inserted USB drive letter in c#? 通过代码,我如何测试硬盘驱动器是否正在睡眠而不会唤醒它 - By code, how can I test if a hard disk drive is sleeping without waking it up 通过代码,我怎样才能让硬盘驱动器进入睡眠状态 - By code, how can I send a hard disk drive to sleep 如何在没有插入磁盘介质的情况下以编程方式区分CD驱动器和DVD驱动器? - How to programmatically differentiate between cd drive and dvd drive without a disk media inserted? 插入USB驱动器的驱动器号 - Getting the Drive letter of USB drive inserted 检测硬盘是否正在被访问 - Detect if hard disk is being accessed or not 如何在服务项目中获得插入的USB驱动器号? - How to get an insterted USB drive letter in a service project? 如何在我的 Google 驱动器中上传文件(pdf 或 png)并获取其 ID - How can I upload a file(pdf or png) in my Google drive and get it's ID 如何获得硬盘驱动器上文件的http地址? 或如何将文件上传到免费的图片托管并以编程方式获取网址? - How do I get a http address for a file on my hard drive? or how do I upload the file to a free image hosting and get the url programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM