2016-05-26 75 views
0

在我的WPF應用程序中,我需要獲取具有特定屬性的Windows服務(〜200)列表。Wpf WMI Win32_Service查詢

   var result = new List<ServicesModel>();      
       ConnectionOptions connOptions = new ConnectionOptions 
       { 
        Impersonation = ImpersonationLevel.Impersonate, 
        EnablePrivileges = true, 
       }; 

       ManagementScope manScope = new ManagementScope([email protected]"\\{System.Environment.MachineName}\ROOT\CIMV2", 
        connOptions); 
       manScope.Connect(); 

       SelectQuery query = new SelectQuery("SELECT * FROM Win32_Service"); 

       using (var searcher = new ManagementObjectSearcher(manScope, query)) 
        foreach (var o in searcher.Get()) 
        { 
         var obj = (ManagementObject) o; 
         ServicesModel service = new ServicesModel 
         { 
          Name = obj["DisplayName"] as string, 
          Path = obj["PathName"] as string, 
          Description = obj["Description"] as string, 
          Pid = Convert.ToInt32(obj["ProcessId"]), 
          Status = obj["State"] as string, 
          StartMode = obj["StartMode"] as string, 
          LogOnAs = obj["StartName"] as string 
         }; 
         result.Add(service); 
        } 
       return result; 

執行此方法大約需要1分鐘左右,並返回不可接受的數據。 看起來像searcher.Get()需要所有的時間... 我可以做些什麼來提高執行/返回時間/性能?

感謝

+0

我爲管理員重新啓動該盒子感到抱歉。 –

+1

搜尋者很慢。你看過這篇文章嗎? http://stackoverflow.com/questions/10502879/wmi-call-takes-too-much-time-when-system-starts-restarts – Tim

+0

謝謝蒂姆,你的評論幫了我很多! –

回答

0

基於我想出了以下的評論,希望它可以幫助別人:

public List<string> GetWindowsServicesList() 
    { 
     var result = new List<string>(); 
     foreach (ServiceController service in ServiceController.GetServices()) 
     { 
      string serviceName = service.ServiceName; 
      result.Add(serviceName); 
     } 
     return result; 
    } 

    public ServicesModel GetServiceProperties(string serviceName) 
    { 
     ServicesModel service = null; 
     string path = $"Win32_Service.Name=\"{serviceName}\""; 

     using (ManagementObject mngObj = new ManagementObject(path)) 
     { 
      service = new ServicesModel 
      { 
       Name = mngObj.GetPropertyValue("Name") as string, 
       Path = mngObj.GetPropertyValue("PathName") as string, 
       Description = mngObj.GetPropertyValue("Description") as string, 
       Pid = Convert.ToInt32(mngObj.GetPropertyValue("ProcessId")), 
       Status = mngObj.GetPropertyValue("State") as string, 
       StartMode = mngObj.GetPropertyValue("StartMode") as string, 
       LogOnAs = mngObj.GetPropertyValue("StartName") as string 
      }; 
     } 
     return service; 
    } 

    public List<ServicesModel> WindowsServicesCollection() 
    { 
     var result = new List<ServicesModel>(); 
     try 
     { 
      var windowsServicesNames = GetWindowsServicesList(); 

      for (int i = 0; i < windowsServicesNames.Count(); i++) 
      { 
       result.Add(GetServiceProperties(windowsServicesNames[i])); 
      } 
     } 
     catch (System.Management.ManagementException ex) 
     { 
     } 
     catch (System.TimeoutException ex) 
     { 
     } 
     return result; 
    } 

工作起來比以前的解決方案快很多,雖然它看起來性能不超一致,它取決於我沒有發現的一些各種因素......