C#小技巧系列之二:获取系统所有服务信息

本文介绍了一个简单的C#程序,用于枚举并显示计算机上所有的系统服务信息。通过使用System.ServiceProcess命名空间下的ServiceController类,程序能够获取每个服务的名称、显示名称及其状态。

说明:本人准备写一些C#有关的小技巧系列文章,这些文章含金量并不高,代码难度不大,不过因为问的次数比较多,从而导致本人决定用自己所知的方式写这一系列文章,可以看做“趣味导学”系列吧。

这是一个获取系统所有服务的程序,为了简化程序,代码仍以控制台形式提供,大家可以另外封装自己希望的功能。

核心代码:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;

namespace Study
{
    
public class ServiceInfo
    
{
        
public void ShowServiceInfo()
        
{
            ServiceController[] services
=System.ServiceProcess.ServiceController.GetServices();
            System.Console.WriteLine(
"Service名称 Service显示名称 Service状态");
            
for (int i = 0; i < services.Length; i++)
            
{
                System.Console.WriteLine(
string.Format("{0} {1} {2}", services[i].ServiceName, services[i].DisplayName, services[i].Status.ToString()));
            }

        }

    }

}

 启动代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace Study
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            ServiceInfo serviceInfo 
= new ServiceInfo();
            serviceInfo.ShowServiceInfo();
            Console.ReadLine();
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值