none
I want to find USB ports in my system and also list portname is connected/not connected RRS feed

  • Question

  • I want c# program to find the list of USB ports in my system displaying the port name which are connected/not connected

    scenario 1

    USB Port found in Machine

    Port1

    Port2

    List of Port Connected

    Port1 Connected

    List of Port Not Connected

    Port2 Not Connected

    I want the source code for this.
    • Edited by Vinay Rajaram Thursday, November 29, 2018 11:24 AM source code was not added
    Thursday, November 29, 2018 11:20 AM

All replies

  • You can use WMI, with Win32_USBHub for example
    Thursday, November 29, 2018 11:39 AM
  • Hi Vinay Rajaram,

    Thank you for posting here.

    For your question, please try the code below.

    using System;
    
    using System.Collections.Generic;
    
    using System.IO;
    
    using System.Linq;
    
    using System.Management;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    namespace test1
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                List<string> alllist = new List<string>();
    
                List<string> conlist = new List<string>();
    
                using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"" "))
    
                {
    
                    using (ManagementObjectCollection Usb = searcher.Get())
    
                    {
    
                 
    
                        foreach (var usb in Usb)
    
                        {
    
                            alllist.Add(usb["Name"].ToString() + "   " + usb["PNPDeviceID"].ToString());
    
                        }
    
                    }
    
                }
    
                using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
    
                {
    
                    using (ManagementObjectCollection Usb = searcher.Get())
    
                    {
    
                        foreach (var usb in Usb)
    
                        {
    
                            conlist.Add(usb["Name"].ToString() + "   " + usb["PNPDeviceID"].ToString());
    
                        }
    
                    }
    
                }
    
                Console.WriteLine("all the  list  of the  usb________________________");
    
                Console.WriteLine();
    
                foreach (var item in alllist)
    
                {
    
                    Console.WriteLine(item);
    
                }
    
                Console.WriteLine();
    
                Console.WriteLine("list of  the  connected usb_________________________");
    
                Console.WriteLine();
    
                foreach (var item in conlist)
    
                {
    
                    Console.WriteLine(item);
    
                }
    
                Console.WriteLine();
    
                Console.WriteLine("list of  the  unconnected usb_________________________");
    
                Console.WriteLine();
    
                var result = alllist.Except(conlist);
    
                foreach (string item in result)
    
                {
    
                    Console.WriteLine(item);
    
                }
    
                Console.ReadKey();
    
              
    
            }
    
        }
    
    }


    Result:

    Best regards,

    Jack J Jun.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 30, 2018 6:33 AM
    Moderator
  • I wanted to know any usb api is there and also i tried the below code but I wanted output like

    Port1 Connected

    Port2 Not connected

    So On but I am not able to get wmi command or usb api so that I get all usb details

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "C:\\Windows\\System32\\wbem\\WMIC.exe";
                string strdec = "Description like 'USB%'";
                string strvl = " GET /VALUE";
                startInfo.Arguments = "path CIM_LogicalDevice where " + @"""" + strdec + @"""" + strvl;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;

                Process processTemp = new Process();
                processTemp.StartInfo = startInfo;
                processTemp.EnableRaisingEvents = true;

                processTemp.Start();
                string output = processTemp.StandardOutput.ReadToEnd();

    Friday, November 30, 2018 8:57 AM