检测可移动介质的弹出/插入

发布于 2024-08-09 12:15:25 字数 1038 浏览 5 评论 0原文

我正在开发一个项目,需要能够检测何时插入或移除 CD 或 USB 驱动器。我发现一些源代码本应执行此操作,但是,当我插入或弹出 CD 时,似乎没有任何反应。

有人可以验证来源是否正确,并给我一些关于我在这里可能做错了什么的指示吗?

public class MyWindow
{
    ManagementEventWatcher w;

    private void MyWindow_Loaded(object sender, RoutedEventArgs e)
    {
        WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2");
        ConnectionOptions opt = new ConnectionOptions();
        opt.EnablePrivileges = true;

        ManagementScope ms = new ManagementScope("root\\CIMV2", opt);

        w = new ManagementEventWatcher(ms, query);

        w.EventArrived += new EventArrivedEventHandler(w_EventArrived);
        w.Start();
    }

    private void w_EventArrived(object sender, EventArrivedEventArgs e)
    {
        PropertyData pd = e.NewEvent.Properties["TargetInstance"];
    }
}

当我在“PropertyData pd = ...”行上设置断点时,当我弹出/插入 CD 时,它永远不会被击中。因为我根本没有搞乱这个,而且我在网上看到的所有示例都只是引用了相同的源代码(有细微的变化)

I am working on a project in which I need to be able to detect when a CD or a USB drive is inserted or removed. I found some source code that was supposed to do this very thing, however, nothing seems to happen when I insert or eject a CD.

Could someone please verify that the source is correct, and give me any pointers as to what I may have done wrong here?

public class MyWindow
{
    ManagementEventWatcher w;

    private void MyWindow_Loaded(object sender, RoutedEventArgs e)
    {
        WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2");
        ConnectionOptions opt = new ConnectionOptions();
        opt.EnablePrivileges = true;

        ManagementScope ms = new ManagementScope("root\\CIMV2", opt);

        w = new ManagementEventWatcher(ms, query);

        w.EventArrived += new EventArrivedEventHandler(w_EventArrived);
        w.Start();
    }

    private void w_EventArrived(object sender, EventArrivedEventArgs e)
    {
        PropertyData pd = e.NewEvent.Properties["TargetInstance"];
    }
}

When I set a breakpoint on the "PropertyData pd = ..." line, it never gets hit when I eject/insert a CD. Since I've not messed with this at all, and all of the examples I've seen online simply cite this same source code (with minor variations)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-08-16 12:15:25
using System.Management;

public void networkDevice()
{
    try
    {
        WqlEventQuery q = new WqlEventQuery();
        q.EventClassName = "__InstanceModificationEvent";
        q.WithinInterval = new TimeSpan(0, 0, 1);
        q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";

        ConnectionOptions opt = new ConnectionOptions();
        opt.EnablePrivileges = true;
        opt.Authority = null;
        opt.Authentication = AuthenticationLevel.Default;
        //opt.Username = "Administrator";
        //opt.Password = "";
        ManagementScope scope = new ManagementScope("\\root\\CIMV2", opt);

        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, q);
        watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
        watcher.Start();
    }
    catch (ManagementException e)
    {
        Console.WriteLine(e.Message);
    }
}

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
    ManagementBaseObject wmiDevice = (ManagementBaseObject)e.NewEvent["TargetInstance"];
    string driveName = (string)wmiDevice["DeviceID"];
    Console.WriteLine(driveName);
    Console.WriteLine(wmiDevice.Properties["VolumeName"].Value);
    Console.WriteLine((string)wmiDevice["Name"]);
    if (wmiDevice.Properties["VolumeName"].Value != null)
        Console.WriteLine("CD has been inserted");
    else
        Console.WriteLine("CD has been ejected");
}
using System.Management;

public void networkDevice()
{
    try
    {
        WqlEventQuery q = new WqlEventQuery();
        q.EventClassName = "__InstanceModificationEvent";
        q.WithinInterval = new TimeSpan(0, 0, 1);
        q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";

        ConnectionOptions opt = new ConnectionOptions();
        opt.EnablePrivileges = true;
        opt.Authority = null;
        opt.Authentication = AuthenticationLevel.Default;
        //opt.Username = "Administrator";
        //opt.Password = "";
        ManagementScope scope = new ManagementScope("\\root\\CIMV2", opt);

        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, q);
        watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
        watcher.Start();
    }
    catch (ManagementException e)
    {
        Console.WriteLine(e.Message);
    }
}

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
    ManagementBaseObject wmiDevice = (ManagementBaseObject)e.NewEvent["TargetInstance"];
    string driveName = (string)wmiDevice["DeviceID"];
    Console.WriteLine(driveName);
    Console.WriteLine(wmiDevice.Properties["VolumeName"].Value);
    Console.WriteLine((string)wmiDevice["Name"]);
    if (wmiDevice.Properties["VolumeName"].Value != null)
        Console.WriteLine("CD has been inserted");
    else
        Console.WriteLine("CD has been ejected");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文