1. 程式人生 > 實用技巧 >c#操作Windows服務的建立,啟動,停止和解除安裝

c#操作Windows服務的建立,啟動,停止和解除安裝

網上看了這個大神的帖子,以為很簡單,自己也想試一下,結果問題多多

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

第一個問題便是:程式碼抄過來後發現這兩句出問題,沒辦法引用,百度了半天都很麻煩,度娘折騰半天也沒方案,後來想以下,說不定nuget有呢,果然試一下就ok了,直接搜尋System.ServiceProcess/Configuration即可搞定

using System.ServiceProcess;

using System.Configuration.Install;

第二個問題是:搞定了程式碼問題,終於可以跑起來了,發現沒辦法安裝服務

大家都知道,windows7/10有時候問題莫名其妙,就用管理員許可權試一下,果然發現,必須用管理員許可權執行,就可以安裝,但是仍然無法啟動服務

第三個問題:服務安裝了,但是仍然沒辦法啟動,停止服務,也沒辦法解除安裝服務。

用管理員執行問題依舊,百度到這個大神https://blog.csdn.net/csethcrm/article/details/17922397,果然,把服務程式的目錄安全,新增一個everyone使用者,許可權設定為完全控制,所有終於搞定了。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text; using System.Threading.Tasks; namespace WindowsService1 { public class ServiceManager { //判斷服務是否存在 public static bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach
(ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安裝服務 public static 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); } } //解除安裝服務 public static void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //啟動服務 public static void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //停止服務 public static void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }
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;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        string filePath = $@"C:\Users\xxx\Desktop\Temp\WindowsFormsApp4\WindowsFormsApp4\bin\Debug\MyServiceLog.txt";
        protected override void OnStart(string[] args)
        {            
            using (FileStream stream = new FileStream(filePath, FileMode.Append))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine($"{DateTime.Now},服務啟動!");
                }
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此處新增程式碼以執行停止服務所需的關閉操作。
            using (FileStream stream = new FileStream(filePath, FileMode.Append))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine($"{DateTime.Now},服務停止!");
                }
            }
        }
    }
}