C# - 在 Windows XP - USB 驱动器上未检测到事件



我是C#的新手 - 三天前开始,因为对USB驱动器的特殊需求。在这里和那里阅读,我可以有以下代码来工作。我想知道用户何时在 USB 端口中插入笔式驱动器。

唯一的问题是,在XP32(我测试的唯一XP)上,永远不会检测到该事件。在Windows 7上,它可以完美运行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Microsoft.Win32;
using System.Security.Permissions;
namespace X
{
    class Program
    {
        static void Main(string[] args)
        {    
            ManagementEventWatcher watcher = new ManagementEventWatcher();
            WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
            watcher.Query = query;
            watcher.Start();
            watcher.WaitForNextEvent();
         // DO something if a pen drive (or any storage device) is inserted.
         // Works fine on Windows 7
         // XP will ignore the event... 
        }
    }
}   

任何建议将非常欢迎!

问候

塞吉奥

看起来你需要XP Service Pack 3才能工作(x64上的SP2)。

最新更新