none
System.Management.dll causing crashes in some of my Windows 7 computer RRS feed

  • Question

  • Hello all,

    Let me start right away with my issue.

    I am trying to list and display all the COM ports available in my pc using c# code ( here is where i use a System.Management.dll which has some API's to segregate between BT and Cable ports).

    Now,when i build and run .The Application crashes in some of my Win 7 PC. 

    After Referring to a number of forums, i included the DLL file in my  .\bin\release and referred to it locally. This worked for some computer but failed in some.

    Since it is System related DLL i am not sure where the problem lies. Please help me .

    P.S : My project uses .NET Framework v4.5 standard and not any Client version.

    I am adding the code snippet below, please refer and provide your suggestions

    /********************/

            string ComPortName = null;
            string[] ArrayComPortsNames;
            string[] ArrayComPortsFullNames = null;
            List<String> ListComPortsFullNames = new List<string>();
            List<String> ListComPortsNames = new List<string>();

                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
                {
                    var portnames = SerialPort.GetPortNames();
                    var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

                    var comportList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();

                    if (comportList.Count == 0)
                    {
                         return;
                    }

                    foreach (string s in comportList)
                    {
                        if (s.Contains("Communications"))
                        {
                            string b = s.Split(new char[] { '-' })[0];
                         }
                        if (!s.Contains("Bluetooth"))
                        {
                            string b = s.Split(new char[] { '-' })[0];
                            ListComPortsFullNames.Add(b.Trim());
                            Console.WriteLine(b);
                        }
                        else
                        {
                            string b = s.Split(new char[] { '-' })[0];
                            ListComPortsNames.Add(b.Trim());
                            Console.WriteLine(b);
                        }
                    }
                    ArrayComPortsFullNames = ListComPortsFullNames.ToArray();
                    ArrayComPortsNames = ListComPortsNames.ToArray();
                    Array.Sort(ArrayComPortsFullNames);
                    Array.Sort(ArrayComPortsNames);
                }

    /**********/


    • Edited by Shvin_iyer Wednesday, December 20, 2017 6:42 AM
    Wednesday, December 20, 2017 6:29 AM

All replies