【问题标题】:C# - Event is not detected on Windows XP - USB driveC# - Windows XP 上未检测到事件 - USB 驱动器
【发布时间】:2012-12-04 20:50:10
【问题描述】:

我是 C# 的新手——因为对 USB 驱动器的特殊需求,我是三天前开始接触 C# 的。在这里和那里阅读我可以使用以下代码。我想知道用户何时将笔式驱动器插入 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... 
        }
    }
}   

欢迎提出任何建议!

问候,

塞尔吉奥

【问题讨论】:

标签: c# windows-7 insert windows-xp usb


【解决方案1】:

Looks like 您需要 XP Service Pack 3 才能工作(x64 上的 SP2)。

【讨论】:

  • Ramhound、gaynorvader 和 DJ KRAZE,感谢您的回复。我的配置是:XP 32 w/SP3,.net fw 3.5。关于这篇文章 (msdn.microsoft.com/en-us/library/windows/desktop/…) 它说最低支持的客户端是 Windows XP [仅限桌面应用程序] 这似乎是错误的。我将我的应用程序转换为 Windows 窗体(也可以作为服务运行),但它不起作用。将仔细查看您的建议并尽快发布结果。我相信这件事会对更多的人有所帮助。问候!
猜你喜欢
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2015-11-21
  • 2023-04-07
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多