Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Now I am able to obtain some information of the usb Disk Drives, but i want to remove the USB, how can I remove the device with a windows service.

I use this code to detect all the USB devices plugged in the computer.

C#
try
{
     ManagementScope sc = new ManagementScope(@"\\" + Environment.MachineName +            @"\root\cimv2");

     ObjectQuery query = new ObjectQuery("Select * from Win32_DiskDrive");
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query);
     ManagementObjectCollection result = searcher.Get();
     String Datos = "";
     String DriveLetter = "N/A";
     foreach (ManagementObject obj in result)
     {
          ObjectQuery query2 = new ObjectQuery( "ASSOCIATORS OF    {Win32_DiskDrive.DeviceID='" + obj["DeviceID"].ToString() +  "'} where AssocClass = Win32_DiskDriveToDiskPartition");
          ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(sc, query2);
          ManagementObjectCollection result2 = searcher2.Get();
                    
          foreach (ManagementObject obj2 in result2)
          {
               query3 = new ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + obj2["DeviceID"].ToString() + "'} where AssocClass = Win32_LogicalDiskToPartition");
               ManagementObjectSearcher searcher3 = new ManagementObjectSearcher(sc, query3);
               ManagementObjectCollection result3 = searcher3.Get();
               foreach (ManagementObject obj3 in result3)
               {
                    DriveLetter = obj3["DeviceID"].ToString();
               }
          }
          if (obj["InterfaceType"] != null)
          {
               String InterfaceType=obj["InterfaceType"].ToString();
               if (InterfaceType == "USB")
               {
                    if (obj["Description"] != null) Datos += "Description:\t" + obj["Description"].ToString() + "\n";
                    if (obj["DeviceID"] != null) Datos += "DeviceID:\t" + obj["DeviceID"].ToString() + "\n";
                    if (obj["PNPDeviceID"] != null) Datos += "PNPDeviceID:\t" + obj["PNPDeviceID"].ToString() + "\n";
                    if (obj["Caption"] != null) Datos += "Caption:\t" + obj["Caption"].ToString() + "\n";
                    if (obj["Status"] != null) Datos += "Status:\t" + obj["Status"].ToString() + "\n";
                    if (obj["InterfaceType"] != null) Datos += "InterfaceType:\t" + obj["InterfaceType"].ToString() + "\n";
                    if (DriveLetter != null) Datos += "DriveLetter:\t" + DriveLetter.ToString() + "\n";
                    Datos += "\n";
               }
          }
     }
     if (Datos!="") eventLogSimple.WriteEntry(Datos, EventLogEntryType.Warning);
}
catch (Exception ex)
{
     eventLogSimple.WriteEntry(ex.Message ,EventLogEntryType.Error);
}
Posted
Updated 18-Mar-15 10:16am
v2
Comments
[no name] 20-Mar-15 5:17am    
What do you mean precisely by remove?
Member 11522982 20-Mar-15 10:12am    
i mean eject safely the usb disk drive
Herman<T>.Instance 20-Mar-15 12:17pm    
Just unplug IT!
[no name] 20-Mar-15 20:04pm    
What was wrong with the link you were given the last time you asked the same question? http://www.codeproject.com/Articles/13530/Eject-USB-disks-using-C

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900