参考
[1]https://blog.csdn.net/qq_39097425/article/details/81912745
程序名是去除exe后缀的名字
相对路径的根目录是工程目录(最高级的目录,与Assets/Library等同级)
- 隐藏窗口
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System;
public class CallApplication : MonoBehaviour
{
// Use this for initialization
private static string outputPath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
private int x = 0, y = 0;
private bool isOn = false;
private int minute = 0;
// Use this for initialization
void Start()
{
//ListAllAppliction();
UnityEngine.Debug.Log("当前应用:" + Process.GetCurrentProcess().ProcessName + " 进程ID: " + Process.GetCurrentProcess().Id);
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "打开外部应用"))
{
if (CheckProcess("chrome"))
return;
else
StartProcess(outputPath);
}
if (GUI.Button(new Rect(10, 60, 200, 50), "杀死应用进程"))
{
KillProcess("chrome");
}
if (GUI.Button(new Rect(10, 110, 200, 50), "开启定时关闭"))
{
minute = System.DateTime.Now.Minute + 1;
isOn = true;
}
if (isOn)
{
GUI.contentColor = Color.red;
GUI.Label(new Rect(10, 160, 160, 30), "\t倒计时:" + (60 - System.DateTime.Now.Second));
if (System.DateTime.Now.Minute == minute)
{
UnityEngine.Debug.Log("自动关闭应用....");
KillProcess("chrome");
isOn = false;
}
}
}
/// <summary>
/// 开启应用
/// </summary>
/// <param name="ApplicationPath"></param>
void StartProcess(string ApplicationPath)
{
UnityEngine.Debug.Log("打开本地应用");
Process foo = new Process();
foo.StartInfo.FileName = ApplicationPath;
foo.Start();
}
/// <summary>
/// 检查应用是否正在运行
/// </summary>
bool CheckProcess(string processName)
{
bool isRunning = false;
Process[] processes = Process.GetProcesses();
int i = 0;
foreach (Process process in processes)
{
try
{
i++;
if (!process.HasExited)
{
if (process.ProcessName.Contains(processName))
{
UnityEngine.Debug.Log(processName + "正在运行");
isRunning = true;
continue;
}
else if (!process.ProcessName.Contains(processName) && i > processes.Length)
{
UnityEngine.Debug.Log(processName + "没有运行");
isRunning = false;
}
}
}
catch (Exception ep)
{
}
}
return isRunning;
}
/// <summary>
/// 列出已开启的应用
/// </summary>
void ListAllAppliction()
{
Process[] processes = Process.GetProcesses();
int i = 0;
foreach (Process process in processes)
{
try
{
if (!process.HasExited)
{
UnityEngine.Debug.Log("应用ID:" + process.Id + "应用名:" + process.ProcessName);
}
}
catch (Exception ep)
{
}
}
}
/// <summary>
/// 杀死进程
/// </summary>
/// <param name="processName">应用程序名</param>
void KillProcess(string processName)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
try
{
if (!process.HasExited)
{
if (process.ProcessName == processName)
{
process.Kill();
UnityEngine.Debug.Log("已杀死进程");
}
}
}
catch (System.InvalidOperationException)
{
//UnityEngine.Debug.Log("Holy batman we've got an exception!");
}
}
}
}