您的位置:首页 > 编程语言 > C#

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

2007-05-07 22:00 417 查看
说明:本人准备写一些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();


}


}


}


本文出自 “周公(周金桥)的专栏” 博客,请务必保留此出处http://zhoufoxcn.blog.51cto.com/792419/164401
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: