C#Windows系统服务管理源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#Windows系统服务管理源代码1.实现的界面2.要导入的命名空间usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.Win32;usingSystem.IO;usingSystem.ServiceProcess;usingSystem.Diagnostics;3.刷新服务功能的实现privatevoid刷新ToolStripMenuItem_Click(objectsender,EventArgse){lstserver.Items.Clear();#region刷新服务watch=System.ServiceProcess.ServiceController.GetServices();foreach(ServiceControllertempinwatch){try{stringservepath=;ListViewItemlv=newListViewItem(temp.DisplayName.ToString());lv.SubItems.Add(temp.ServiceName.ToString());#region1.服务状态if(temp.Status.ToString()==Stopped){lv.SubItems.Add(已停止);}elseif(temp.Status.ToString()==Running){lv.SubItems.Add(正在运行);}else{lv.SubItems.Add(已暂停);}#endregion#region2.服务启动类型try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());stringregs=hklmrun2.GetValue(start).ToString();if(regs==3){lv.SubItems.Add(手动);}elseif(regs==4){lv.SubItems.Add(已禁用);}elseif(regs==2){lv.SubItems.Add(自动);}elseif(regs==0){lv.SubItems.Add(自动);}else{lv.SubItems.Add(未知);}}catch{}#endregion#region3.服务描述try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());stringregs=hklmrun2.GetValue(Description).ToString();lv.SubItems.Add(regs);}catch{lv.SubItems.Add();}#endregion#region4.服务路径try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());servepath=hklmrun2.GetValue(ImagePath).ToString();lv.SubItems.Add(servepath);}catch{lv.SubItems.Add();}#endregion#region5.服务厂商stringfileinfo=;try{FileVersionInfofinfo=FileVersionInfo.GetVersionInfo(@servepath);fileinfo=finfo.CompanyName;lv.SubItems.Add(fileinfo);}catch{lv.SubItems.Add();}if(fileinfo!=MicrosoftCorporation&&fileinfo!=){lv.ForeColor=Color.Blue;}#endregionlstserver.Items.Add(lv);}catch{}}#endregionlblservecount.Text=当前服务:+lstserver.Items.Count.ToString()+项;}4.显示已启动服务功能的实现privatevoid显示已启动服务ToolStripMenuItem_Click(objectsender,EventArgse){lstserver.Items.Clear();#region刷新服务watch=System.ServiceProcess.ServiceController.GetServices();foreach(ServiceControllertempinwatch){#regionforeachtry{stringserverrunning=;serverrunning=temp.Status.ToString();if(serverrunning==Running){try{stringservepath=;ListViewItemlv=newListViewItem(temp.DisplayName.ToString());lv.SubItems.Add(temp.ServiceName.ToString());#region1.服务状态if(temp.Status.ToString()==Stopped){lv.SubItems.Add(已停止);}elseif(temp.Status.ToString()==Running){lv.SubItems.Add(正在运行);}else{lv.SubItems.Add(已暂停);}#endregion#region2.服务启动类型try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());stringregs=hklmrun2.GetValue(start).ToString();if(regs==3){lv.SubItems.Add(手动);}elseif(regs==4){lv.SubItems.Add(已禁用);}elseif(regs==2||regs==0){lv.SubItems.Add(自动);}else{lv.SubItems.Add(未知);}}catch{}#endregion#region3.服务描述try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());stringregs=hklmrun2.GetValue(Description).ToString();lv.SubItems.Add(regs);}catch{lv.SubItems.Add();}#endregion#region4.服务路径try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());servepath=hklmrun2.GetValue(ImagePath).ToString();lv.SubItems.Add(servepath);}catch{lv.SubItems.Add();}#endregion#region5.服务厂商stringfileinfo=;try{FileVersionInfofinfo=FileVersionInfo.GetVersionInfo(@servepath);fileinfo=finfo.CompanyName;lv.SubItems.Add(fileinfo);}catch{lv.SubItems.Add();}if(fileinfo!=MicrosoftCorporation&&fileinfo!=){lv.ForeColor=Color.Blue;}#endregionlstserver.Items.Add(lv);}catch{}}}catch{}#endregion}#endregionlblservecount.Text=当前服务:+lstserver.Items.Count.ToString()+项;}5.显示未启动服务功能的实现privatevoid显示未启动服务ToolStripMenuItem_Click(objectsender,EventArgse){lstserver.Items.Clear();#region刷新服务watch=System.ServiceProcess.ServiceController.GetServices();foreach(ServiceControllertempinwatch){#regionforeachtry{stringserverrunning=;serverrunning=temp.Status.ToString();if(serverrunning!=Running){try{stringservepath=;ListViewItemlv=newListViewItem(temp.DisplayName.ToString());lv.SubItems.Add(temp.ServiceName.ToString());#region1.服务状态if(temp.Status.ToString()==Stopped){lv.SubItems.Add(已停止);}elseif(temp.Status.ToString()==Running){lv.SubItems.Add(正在运行);}else{lv.SubItems.Add(已暂停);}#endregion#region2.服务启动类型try{RegistryKeyhklmrun1=Registry.LocalMachine;RegistryKeyhklmrun2=hklmrun1.CreateSubKey(@system\currentcontrolset\services\+temp.ServiceName.ToString());stringregs=hklmrun2.GetValue(start).ToString();if(regs==3){lv.SubItems.Add(手动);}elseif(regs==4){lv.SubItems.Add(已禁用);}elseif(regs==2||regs==0){lv.SubItems.Add(自动);}else{lv.SubItems.Add(未知);}}ca

1 / 26
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功