C#开发Windows服务守护程序
背景
通常为了便于对指定Windows服务进行方便的管理,我们会开发一个守护程序,或者叫做服务管理的工具,基本要实现windows服务的安装、卸载、启动、停止。
实现关键
System.ServiceProcess.ServiceController //使用此类访问已安装的Windows服务
C#编程判断指定的Windows服务是否存在
/// <summary>
/// 判断指定的服务是否已存在(安装)
/// </summary>
/// <param name="serviceName">要判断服务名称</param>
/// <returns>存在(安装)返回true,不存在(未安装)返回false</returns>
public bool IsServiceExists(string serviceName)
{
bool exists = false;
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
exists = true;
break;
}
}
return exists;
}
C#编程获取指定Windows服务状态
/// <summary>
/// 获取windows服务的状态
/// </summary>
/// <param name="serviceName">要获取状态的服务名称</param>
/// <returns>返回服务状态</returns>
public string GetServiceStatus(string serviceName)
{
string result = "服务不存在";
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
result = s.Status.ToString();
break;
}
}
return result;
}
C#编程实现服务安装、启动、停止、卸载
1、定义变量
private string serivceExeFullPath = @"D:\BTSService\BTSDataService.exe"; //服务程序的完整路径
private string serviceName = "BTSDataService"; //服务名称
2、服务安装(本Windows服务是基于Topshelf框架开发)
private void btnServiceInstall_Click(object sender, EventArgs e)
{
if (!System.IO.File.Exists(serivceExeFullPath))
{
MessageBox.Show("服务路径不存在,请确认服务程序是否存在!");
return;
}
if (this.IsServiceExists(this.serviceName))
{
MessageBox.Show("服务已安装!");
return;
}
try
{
using(System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "install";
p.Start();
p.Close();
}
System.Threading.Thread.Sleep(2000);
this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);
MessageBox.Show("服务安装完毕!");
}
catch(Exception ex)
{
MessageBox.Show("服务安装异常:" + ex.Message);
}
}
3、服务启动(本Windows服务是基于Topshelf框架开发)
private void btnServiceStart_Click(object sender, EventArgs e)
{
if (!this.IsServiceExists(this.serviceName))
{
MessageBox.Show("服务未安装,请先安装!");
return;
}
try
{
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "start";
p.Start();
p.Close();
}
System.Threading.Thread.Sleep(2000);
this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);
MessageBox.Show("服务启动成功!");
}
catch (Exception ex)
{
MessageBox.Show("服务安装异常:" + ex.Message);
}
}
4、服务停止(本Windows服务是基于Topshelf框架开发)
private void btnServiceStop_Click(object sender, EventArgs e)
{
if (!this.IsServiceExists(this.serviceName))
{
MessageBox.Show("服务未安装,请先安装!");
return;
}
try
{
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "stop";
p.Start();
p.Close();
}
System.Threading.Thread.Sleep(2000);
this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);
MessageBox.Show("服务停止成功!");
}
catch (Exception ex)
{
MessageBox.Show("服务安装异常:" + ex.Message);
}
}
5、服务卸载(本Windows服务是基于Topshelf框架开发)
private void btnServiceUninstall_Click(object sender, EventArgs e)
{
if (!this.IsServiceExists(this.serviceName))
{
MessageBox.Show("服务未安装,请先安装!");
return;
}
try
{
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = serivceExeFullPath;
p.StartInfo.Arguments = "uninstall";
p.Start();
p.Close();
}
System.Threading.Thread.Sleep(2000);
this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);
MessageBox.Show("服务卸载成功!");
}
catch (Exception ex)
{
MessageBox.Show("服务安装异常:" + ex.Message);
}
}
说明:对于守护程序通常是要定义检测服务的运行状态的,可以根据业务需要自己进行改进。