Detecting Eject/Insert of Removeable Media

前端 未结 1 728
说谎
说谎 2021-01-06 04:59

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 th

相关标签:
1条回答
  • 2021-01-06 05:47
    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");
    }
    
    0 讨论(0)
提交回复
热议问题