c# winform 安装启动windows服务示例
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; using System.Collections; namespace LRDDIServiceClient { public partial class Form1 : Form { //安装及卸载路径 string serviceFilePath = $"{Application.StartupPath}\\LRDDIService.exe"; //服务名称 string serviceName = "serviceI程序"; public Form1() { InitializeComponent(); } /// <summary> /// 判断服务是否存在 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { MessageBox.Show(sc.ServiceName.ToLower()); return true; } } return false; } /// <summary> /// 安装服务 /// </summary> /// <param name="serviceFilePath"></param> private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary saveState = new Hashtable(); installer.Install(saveState); installer.Commit(saveState); } } /// <summary> /// 卸载服务 /// </summary> /// <param name="serviceFilePath"></param> private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } /// <summary> /// 启动服务 /// </summary> /// <param name="serviceName"></param> private void ServiceStart(string serviceName) { using (ServiceController controller = new ServiceController(serviceName)) { if (controller.Status == ServiceControllerStatus.Stopped) { controller.Start(); } } } /// <summary> /// 停止服务 /// </summary> /// <param name="serviceName"></param> private void ServiceStop(string serviceName) { using (ServiceController controller = new ServiceController(serviceName)) { if (controller.Status == ServiceControllerStatus.Running) { controller.Stop(); } } } /// <summary> /// 事件:安装服务 /// </summary> private void button1_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } /// <summary> /// 停止服务 /// </summary> private void button2_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } /// <summary> /// 停止服务 /// </summary> private void button3_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } /// <summary> /// 卸载服务 /// </summary> private void button4_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } } }
通过 Form1启动服务后,每隔一分钟刷新一次,如果时间是:16:10分,则将日志写入d:\log.txt文件;(button 1-4 )

service1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; namespace LRDDIService { public partial class Service1 : ServiceBase { Timer timer; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer = new Timer(); timer.Interval = 60000;// 60 seconds 60秒执行一次 timer.Elapsed += new ElapsedEventHandler(this.OnTimer); timer.Start(); } /// <summary> /// 定时器中定时执行的任务 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void OnTimer(object sender, ElapsedEventArgs args) { // TODO: Insert monitoring activities here. if (DateTime.Now.Hour.ToString() == "16" && DateTime.Now.Minute.ToString()=="10") { log("任务开始了"); } } /// <summary> /// 停止服务 /// </summary> protected override void OnStop() { log("In OnStop."); } /// <summary> /// 停止服务 /// </summary> /// <summary> /// 记录到指定路径:D:\log.txt /// </summary> /// <param name="message"></param> private static void log(string message) { using (FileStream stream = new FileStream("D:\\log.txt", FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now}:{message}"); } } } }
活到老,学到老。

浙公网安备 33010602011771号