我想在远程机器上重新启动服务并且不想使用 ServiceController,因为在该机器上获取所有服务的过程需要 21 秒,而以下 ManagementObject 在不到 2 秒内返回:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + ConfigurationManager.AppSettings["remoteMachine"] + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_Service where DisplayName LIKE '%" + ConfigurationManager.AppSettings["likeSerices"] + "%'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<ServiceObj> outList = new List<ServiceObj>();
foreach (ManagementObject m in queryCollection)
{
ServiceObj thisObject = new ServiceObj();
thisObject.DisplayName = m["DisplayName"].ToString();
thisObject.Name = m["Name"].ToString();
thisObject.Status = m["State"].ToString();
thisObject.StartMode = m["StartMode"].ToString();
outList.Add(thisObject);
}
我现在尝试了:m.InvokeMethod("StopService", null); 在 foreach 块中没有成功。我在做什么?
谢谢杰克