查询Windows Service的所有数据
需要引入using System.ServiceProcess
ServiceController[] Services = ServiceController.GetServices();
for (int i = 0; i < Services.Length; i++)
{
if (Services[i].ServiceType == ServiceType.Win32ShareProcess
|| Services[i].ServiceType == ServiceType.Win32OwnProcess
|| Services[i].ServiceType == ServiceType.InteractiveProcess)
{
perMessage_Convertor _perMessage_Convertor = new perMessage_Convertor();
_perMessage_Convertor.ColorValue = ((int)Services[i].Status).ToString();
_perMessage_Convertor.Name = Services[i].ServiceName;
_perMessage_Convertor.Status = Services[i].Status.ToString();
_perMessage_Convertor.StartMode = GetStartMode(_perMessage_Convertor.Name);
_perMessage_Convertor.LogOnAs = Services[i].MachineName;
//_perMessage_Convertor.Description = Services[i].DisplayName;
_perMessage_Convertor.Description = GetServicePath(_perMessage_Convertor.Name);
_perMessage_Convertor.Path = GetServicePath(_perMessage_Convertor.Name);
MessageConvertorALL.Add(_perMessage_Convertor);
}
}
通过Win32_Service查询部分ServiceController无法查询的内容
public static string GetServicePath(string name)
{
string query = string.Format("Select PathName from Win32_Service where Name='{0}'", name);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject o in searcher.Get())
{
return o["PathName"].ToString();
}
return string.Format("Service {0} not found", name);
}
Win32_Service可查询的内容如下
[Dynamic, Provider("CIMWin32"), SupportsUpdate, UUID("{8502C4D9-5FBB-11D2-AAC1-006008C78BC7}"), DisplayName("Services"), AMENDMENT]
class Win32_Service : Win32_BaseService
{
boolean AcceptPause;
boolean AcceptStop;
string Caption;
uint32 CheckPoint;
string CreationClassName;
boolean DelayedAutoStart;
string Description;
boolean DesktopInteract;
string DisplayName;
string ErrorControl;
uint32 ExitCode;
datetime InstallDate;
string Name;
string PathName;
uint32 ProcessId;
uint32 ServiceSpecificExitCode;
string ServiceType;
boolean Started;
string StartMode;
string StartName;
string State;
string Status;
string SystemCreationClassName;
string SystemName;
uint32 TagId;
uint32 WaitHint;
};
其他系统可查询内容请参照:
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-service