工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

可以通过选择菜单“开始”->“控制面板”->“管理工具”->“服务”来查看系统中的服务,如下图所示.

 

在项目中引入“System.ServiceProcess.dll”,在类中引入命名空间:

using System.ServiceProcess;

 例子:效果如下图,显示系统所有running的信息,如下图。

页面源代码:

View Code
 1  <asp:GridView ID="gv_state" runat="server" GridLines="None" BorderWidth="0px" CellPadding="0"
 2             CellSpacing="1" align="center" AutoGenerateColumns="false">
 3             <Columns>
 4                 <asp:TemplateField HeaderText="标识名称">
 5                     <ItemTemplate>
 6                         <%#((System.Data.DataRowView)Container.DataItem)["ServiceName"]%>
 7                     </ItemTemplate>
 8                     <ItemStyle Width="20%" />
 9                 </asp:TemplateField>
10                 <asp:TemplateField HeaderText="服务类型">
11                     <ItemTemplate>
12                         <%#((System.Data.DataRowView)Container.DataItem)["ServiceType"]%>
13                     </ItemTemplate>
14                     <ItemStyle Width="20%" />
15                 </asp:TemplateField>
16                 <asp:TemplateField HeaderText="友好名称">
17                     <ItemTemplate>
18                         <%#((System.Data.DataRowView)Container.DataItem)["DisplayName"] %>
19                     </ItemTemplate>
20                     <ItemStyle Width="20%" />
21                 </asp:TemplateField>
22                 <asp:TemplateField HeaderText="服务状态">
23                     <ItemTemplate>
24                         <%#((System.Data.DataRowView)Container.DataItem)["Status"] %>
25                     </ItemTemplate>
26                     <ItemStyle Width="20%" />
27                 </asp:TemplateField>
28             </Columns>
29             <RowStyle CssClass="tr3" Font-Size="12px" Height="28px" />
30             <HeaderStyle CssClass="itable_title" />
31             <EmptyDataTemplate>
32                 <tr class="itable_title">
33                     <th width="25%">
34                         标识名称
35                     </th>
36                     <th width="25%">
37                         服务类型
38                     </th>
39                     <th width="25%">
40                         友好名称
41                     </th>
42                     <th width="25%">
43                         服务状态
44                     </th>
45                 </tr>
46                 <tr class="tr3">
47                     <td class="grid_no_result" colspan="4">
48                         <span>当前没有查询记录</span>
49                     </td>
50                 </tr>
51             </EmptyDataTemplate>
52         </asp:GridView>

后台代码:

View Code
 1  protected void Page_Load(object sender, EventArgs e)
 2         {
 3             if (!IsPostBack)
 4             {
 5                 ServiceController[] controller = ServiceController.GetServices();
 6                 DataTable dt = CreateDataTable();
 7                 for (int i = 0; i < controller.Length; i++)
 8                 {
 9                     if (controller[i].Status == ServiceControllerStatus.Running)
10                     {
11                         DataRow dr = dt.NewRow();
12                         dr["ServiceName"] = controller[i].ServiceName;
13                         dr["ServiceType"] = controller[i].ServiceType;
14                         dr["DisplayName"] = controller[i].DisplayName;
15                         dr["Status"] = controller[i].Status;
16                         dt.Rows.Add(dr);
17                     }
18                 }
19                 this.gv_state.DataSource = dt;
20                 this.gv_state.DataBind();
21             }
22         }
23 
24         public DataTable CreateDataTable()
25         {
26             DataTable dt = new DataTable();
27             dt.Columns.Add("ServiceName", typeof(string));
28             dt.Columns.Add("ServiceType", typeof(string));
29             dt.Columns.Add("DisplayName", typeof(string));
30             dt.Columns.Add("Status", typeof(string));
31             return dt;
32         }

 

posted on 2012-07-20 08:23  梦里故乡  阅读(295)  评论(0编辑  收藏  举报