connected usb devices in c#

-2

hello i tried a tutorial which i found it here for discovering all connected usb devices in c# the tutorial which i see is Get List of connected USB Devices and vs throw this error message when i debug it,i did a lot of searches on google whitout any result please if someone know to fix it, i will be thankeful. Thanks by advance.

the error message is :

    System.Runtime.InteropServices.COMException
  HResult=0x80070424
  Message=Le service spécifié n’existe pas en tant que service installé. (Exception from HRESULT: 0x80070424)
  Source=mscorlib
  StackTrace:
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObjectSearcher.Initialize()
   at System.Management.ManagementObjectSearcher.Get()
   at An.Program.GetUSBDevices() in C:\Users\anas\source\repos\An\An\Program.cs:line 32
   at An.Program.Main(String[] args) in C:\Users\anas\source\repos\An\An\Program.cs:line 15

the code source is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Management;
using System.Management.Instrumentation;

namespace An
{
    class Program
    {
        static void Main(string[] args)
        {
            var usbDevices = GetUSBDevices();

            foreach (var usbDevice in usbDevices)
            {
                Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                    usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
            }

            Console.Read();
        }

        static List<USBDeviceInfo> GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity  "))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }

            collection.Dispose();
            return devices;
        }
    }

    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }
}
c#
usb
asked on Stack Overflow Oct 4, 2018 by anas boussib • edited Oct 5, 2018 by anas boussib

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0