Windows Service 開啓和禁用

//打包出去纔可以用必須以管理員方式運行

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using System.ServiceProcess;  //需要從Unity裏面導出dll
using UnityEngine.UI;
public class Startfuwu : MonoBehaviour
{
    Text textService;
    private void Start()
    {
        textService = gameObject.GetComponent<Text>();
    }
    /// <summary>
    /// 開啓服務
    /// </summary>
    /// <param name="windowsServiceName">服務名稱</param>
    private void StartWindowsService(string windowsServiceName)
    {
        using (ServiceController control = new ServiceController(windowsServiceName))
        {
            if (control.Status == ServiceControllerStatus.Stopped)
            {
                textService.text=("服務啓動......");
                control.Start();
                textService.text = ("服務已經啓動......");
            }
            else if (control.Status == ServiceControllerStatus.Running)
            {
                textService.text = ("服務已經啓動......");
            }
        }

    }
    /// <summary>
    /// 停止服務
    /// </summary>
    /// <param name="windowsServiceName">服務名稱</param>
    private void StopWindowsService(string windowsServiceName)
    {
        using (ServiceController control = new ServiceController(windowsServiceName))
        {
            if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
                textService.text = ("服務停止......");
                control.Stop();
                textService.text = ("服務已經停止......");
            }
            else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
            {
                textService.text = ("服務已經停止......");
            }
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            StartWindowsService("Application Service");
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            StopWindowsService("Application Service");
        }
    }

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章