C# 程序查询 Windows Service相关信息

3 篇文章 0 订阅
2 篇文章 0 订阅

查询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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值