2016-05-26 6 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分かかります。 実行/戻り時間/パフォーマンスを改善するために何ができるのですか?私は、次のを思い付いたコメントに基づいて

おかげ

+0

私はそのボックスを再起動する必要がある管理者のために申し訳ありません。 –

+1

検索者が非常に遅いです。あなたはこの投稿を見ましたか? http://stackoverflow.com/questions/10502879/wmi-call-takes-to-much-time-when-system-starts-restarts – Tim

+0

ありがとうTim、あなたのコメントが私を助けてくれました! –

答えて

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; 
    } 

がはるかに速く、以前のソリューションよりも、ずっと作品はスーパー一貫していないとそれは私が特定しなかったいくつかの様々な要因によります...

関連する問題