2016-03-08 18 views
-1

我有一個逗號分隔列出了我需要停止的服務名稱,但在此之前我想檢查它們是否都存在於系統中。我怎樣才能得到那些使用LINQ不存在的服務的名稱。獲取在PC上不存在的服務列表

我看着這一個,但它只檢查一個服務名稱不是完整的列表。 Check if a service exists on a particular machine without using exception handling

+0

我想你可以得到服務列表,然後拆分字符串以獲取它是否包含在服務列表中 – Pankaj

+0

嘗試編碼某些內容,然後在遇到問題時再回來。 – Will

回答

1
ServiceController[] services = ServiceController.GetServices(); 
var availableServices = services.Select(service => service.ServiceName).ToList(); 

// This is the comma separated List 
string commaSeperatedList = "Service1,Service2,Service3"; 
var servicesToStop = commaSeperatedList.Split(','); 
List<string> notPresentServices = servicesToStop.Where(serviceToStop => availableServices.Contains(serviceToStop) == false).ToList(); 

我們只是檢查,從一個列表中的項目不包含在另一個列表。

相關問題