c# service

https://www.cnblogs.com/cncc/p/7170951.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;

namespace WindowsServiceClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serviceFilePath = $"{Application.StartupPath}\Service1.exe";
string serviceName = "Service1";

//事件:安装服务


private void button1_Click(object sender, EventArgs e)
{
try
{
serviceFilePath = tb_path.Text.ToString().Trim();
if (serviceFilePath == "")
{
MessageBox.Show("请输入服务所在路径!");
return;
}
string[] strs = serviceFilePath.Split('\');
serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
tb_name.Text = serviceName;
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
this.InstallService(serviceFilePath);
MessageBox.Show("安装成功!");
}
catch (Exception ex)
{
MessageBox.Show("安装失败!" + ex.ToString());
}


}

//事件:启动服务
private void button2_Click(object sender, EventArgs e)
{
try
{
serviceFilePath = tb_path.Text.ToString().Trim();
if (serviceFilePath == "")
{
MessageBox.Show("请输入服务所在路径!");
return;
}
string[] strs = serviceFilePath.Split('\');
serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
tb_name.Text = serviceName;
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
MessageBox.Show("启动成功!");
}
catch (Exception ex)
{
MessageBox.Show("启动失败!" + ex.ToString());
}
}

//事件:停止服务
private void button3_Click(object sender, EventArgs e)
{
try
{
serviceFilePath = tb_path.Text.ToString().Trim();
if (serviceFilePath == "")
{
MessageBox.Show("请输入服务所在路径!");
return;
}
string[] strs = serviceFilePath.Split('\');
serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
tb_name.Text = serviceName;
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
MessageBox.Show("停止成功!");
}
catch (Exception ex)
{
MessageBox.Show("停止失败!" + ex.ToString());
}
}

//事件:卸载服务
private void button4_Click(object sender, EventArgs e)
{
try
{
serviceFilePath = tb_path.Text.ToString().Trim();
if (serviceFilePath == "")
{
MessageBox.Show("请输入服务所在路径!");
return;
}
string[] strs = serviceFilePath.Split('\');
serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
tb_name.Text = serviceName;
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
MessageBox.Show("卸载成功!");
}
catch (Exception ex)
{
MessageBox.Show("卸载失败!" + ex.ToString());
}
}

//判断服务是否存在
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}

//安装服务
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
}

//卸载服务
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
}

//启动服务
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
}

//停止服务
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
}

}

原文地址:https://www.cnblogs.com/Apeak/p/14917800.html