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

上传人:b****6 文档编号:6676091 上传时间:2023-01-08 格式:DOCX 页数:17 大小:93.96KB
下载 相关 举报
C#Windows系统服务管理源代码.docx_第1页
第1页 / 共17页
C#Windows系统服务管理源代码.docx_第2页
第2页 / 共17页
C#Windows系统服务管理源代码.docx_第3页
第3页 / 共17页
C#Windows系统服务管理源代码.docx_第4页
第4页 / 共17页
C#Windows系统服务管理源代码.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

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

《C#Windows系统服务管理源代码.docx》由会员分享,可在线阅读,更多相关《C#Windows系统服务管理源代码.docx(17页珍藏版)》请在冰豆网上搜索。

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

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;

}

#endregion

lstserver.Items.Add(lv);

}

catch

{

}

}

#endregion

lblservecount.Text="当前服务:

"+lstserver.Items.Count.ToString()+"项";

}

4.显示已启动服务功能的实现

privatevoid显示已启动服务ToolStripMenuItem_Click(objectsender,EventArgse)

{

lstserver.Items.Clear();

#region刷新服务

watch=System.ServiceProcess.ServiceController.GetServices();

foreach(ServiceControllertempinwatch)

{

#regionforeach

try

{

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;

}

#endregion

lstserver.Items.Add(lv);

}

catch

{

}

}

}

catch

{

}

#endregion

}

#endregion

lblservecount.Text="当前服务:

"+lstserver.Items.Count.ToString()+"项";

}

5.显示未启动服务功能的实现

privatevoid显示未启动服务ToolStripMenuItem_Click(objectsender,EventArgse)

{

lstserver.Items.Clear();

#region刷新服务

watch=System.ServiceProcess.ServiceController.GetServices();

foreach(ServiceControllertempinwatch)

{

#regionforeach

try

{

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;

}

#endregion

lstserver.Items.Add(lv);

}

catch

{

}

}

}

catch

{

}

#endregion

}

#endregion

lblservecount.Text="当前服务:

"+lstserver.Items.Count.ToString()+"项";

}

6.显示自动启动服务功能的实现

privatevoid显示自动启动服务ToolStripMenuItem_Click(objectsender,EventArgse)

{

lstserver.Items.Clear();

#region刷新服务

watch=System.ServiceProcess.ServiceController.GetServices();

foreach(ServiceControllertempinwatch)

{

#regionforeach

try

{

RegistryKeyhklmrun11=Registry.LocalMachine;

RegistryKeyhklmrun21=hklmrun11.CreateSubKey(@"system\currentcontrolset\services\"+temp.ServiceName.ToString());

stringserverrunning=hklmrun21.GetValue("start").ToString();

if(serverrunning=="2"||serverrunning=="0")

{

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

{

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 小学作文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1