Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 466217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:27:24+00:00 2026-05-12T23:27:24+00:00

I am working on a project in which I need to be able to

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:27:24+00:00Added an answer on May 12, 2026 at 11:27 pm
    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in a project which needs to clean up some third party XSD
I'm working on a project in which we will need to determine certain types
I'm working on a little simulation project which uses Lua to drive the behavior
I'm working on a project which is just about to start, and since I
I am working on a project which uses an import #import progid:Blah.blah.retrieve rename_namespace(Blah) but
I'm working on a project which stores single images and text files in one
I'm working on a project which I'm really not sure how to unit test.
I am working on a project which was developed in Visual Studio 2005 (.NET
I'm looking at working on a project which uses C#.NET (sitting on a windows
I am currently working on a project which requires migration of content from different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.