About get ManagementObject int ManagementObjectCollection by foreach is too slowly

0

I want to get port name, but When I change another product, the serial port name also changes. So I need Location Information or Address to get the port name, here the problem, it is too slowly to get the Location Information.

there are the code:

(1) string ComputerName = "localhost";
(2) ManagementScope Scope= new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
(3) Scope.Connect();
(4) ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PnPSignedDriver");
(5) ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
(6) var result = Searcher.Get();
(7) foreach (ManagementObject WmiObject in result)
    {....}

I find step (1)-(6) are vrey fast, but step 7 is extremely slow, it takes 10s! I search in google and try many solutions, still can't solve it. Someone can help me?

jzian

Posted 2019-08-17T17:58:26.120

Reputation: 1

1

Hard to guess not knowing the foreach body however I'd primarily narrow initial result count using WQL where clause.

– JosefZ – 2019-08-17T19:09:02.847

I have tried this method,for example 'ObjectQuery Query = new ObjectQuery(@"SELECT * FROM Win32_PnPSignedDriver where Location = ""Port_#0003.Hub_#0001""");' – jzian – 2019-08-18T02:39:26.940

I have tried this method, but it also slowly. Maybe my code is not best way to do it.Can you show you code to me, and how it work @JosefZ – jzian – 2019-08-18T02:46:45.170

It is the most time-consuming when calculating the result.Count, how can we avoid this step?@JosefZ – jzian – 2019-08-18T02:52:17.687

No answers