参考:
执行步骤 点击打开链接
界面程序代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (ServiceHelper.Status("test_service").ToString() == "NotExist")
{
//安装
ServiceHelper.Install(
"test_service", // 服务名
"test_service", // 显示名称
@"""" + Application.ExecutablePath + @""" service", // 映像路径,可带参数,若路径有空格,需给路径(不含参数)套上双引号
"Frp Windows客户端", // 服务描述
ServiceStartType.Auto, // 启动类型
ServiceAccount.LocalSystem, // 运行帐户,可选,默认是LocalSystem,即至尊帐户
null // 依赖服务,要填服务名称,没有则为null或空数组,可选
);
ServiceHelper.Restart("test_service");
WriteToFile("已注册到系统服务,并启动(控制台无输出)..." + "\r\n");
}
else
{
//卸载
ServiceHelper.Uninstall("test_service");
WriteToFile("已删除系统服务,因系统机制,如重新注册需重启本程序(或exploere.exe)..." + "\r\n");
}
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_form" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
string serviceName = "Service2";
string serviceFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\WindowsService2.exe";
private void button2_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.UninstallService(serviceName);
}
this.InstallService(serviceFilePath) ;
}
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
}
private void button4_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
}
private void button5_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
}
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();
}
}
}
}
}
服务工程代码
using System;
using System.Diagnostics;
using System.ServiceProcess;
using static System.Net.Mime.MediaTypeNames;
namespace WindowsService2
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
Process frp_process = null;
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
wLog("Service Start");
startFrp();
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
CloseFrp();
wLog("Service Stop\n");
}
private void startFrp(Object sender = null, EventArgs e = null)
{
wLog("Run frpc.exe");
frp_process = new Process();
frp_process.StartInfo.FileName = "frpc.exe";
frp_process.StartInfo.Arguments = " -c frpc.ini";
frp_process.StartInfo.CreateNoWindow = true;
frp_process.StartInfo.UseShellExecute = false;
// 守护重启
frp_process.EnableRaisingEvents = true;
frp_process.Exited += new EventHandler(startFrp);
// 进程输出
frp_process.StartInfo.RedirectStandardOutput = true;
frp_process.StartInfo.RedirectStandardError = true;
frp_process.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
frp_process.ErrorDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
frp_process.Start();
frp_process.BeginOutputReadLine();
frp_process.BeginErrorReadLine();
}
private void CloseFrp()
{
wLog("Kill frpc.exe");
if (null == frp_process)
return;
frp_process.Kill();
frp_process.Close();
frp_process = null;
}
private void wLog(string logStr, bool wTime = true)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\autoService.log", true))
{
string timeStr = wTime == true ? DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ") : "";
sw.WriteLine(timeStr + logStr);
}
}
private void MyProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
wLog(outLine.Data, false);
}
}
}
}
1、新建windows service 工程,参考顶部链接,将该工程编译为exe
2、新建windows form 工程,工程代码参考以上
注意:为了便于理解,建2个工程,windows form 工程调用 服务工程exe文件,实现安装、启动、停止、卸载服务。