//打包出去纔可以用必須以管理員方式運行
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");
}
}
}