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