//打包出去才可以用必须以管理员方式运行
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");
}
}
}