Unity 通過程序開關閉外部程式
阿新 • • 發佈:2018-12-22
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; public class BtnCtr : MonoBehaviour { [DllImport("User32.dll", EntryPoint = "FindWindow")] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); public void btn4() { // IntPtr maindHwnd = FindWindow( "WeChatLoginWndForPC", null); //獲得微信登陸框的控制代碼 IntPtr maindHwnd = FindWindow("WeChatMainWndForPC", null); //獲得微信登陸框的控制代碼 if (maindHwnd != IntPtr.Zero) { Process[] ps = Process.GetProcessesByName("WeChat"); if (ps != null) { foreach (Process p in ps) { p.Kill(); } } } else { Process p = new Process(); p.StartInfo.FileName = @"D:\WeiChat\WeChat\WeChat.exe"; p.Start(); } } public void btn5() { IntPtr maindHwnd = FindWindow(null, "TIM"); //獲得Tim登陸框的控制代碼 if (maindHwnd != IntPtr.Zero) { Process[] ps = Process.GetProcessesByName("TIM"); if (ps != null) { foreach (Process p in ps) { p.Kill(); } } } else { Process p = new Process(); p.StartInfo.FileName = @"D:\Tim\Bin\QQScLauncher.exe"; p.Start(); } } public void btn6() { IntPtr maindHwnd = FindWindow("MSPaintApp", null); //獲得電腦畫板登陸框的控制代碼 if (maindHwnd != IntPtr.Zero) { Process[] ps = Process.GetProcessesByName("mspaint"); if (ps != null) { foreach (Process p in ps) { p.Kill(); } } } else { Process p = new Process(); p.StartInfo.FileName = @"C:\WINDOWS\system32\mspaint.exe"; // 電腦自帶的畫圖軟體 p.Start(); } } static void LaunchCommandLineApp() { Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/c" + "start D:/Tim/Bin/QQScLauncher.exe"; process.StartInfo.UseShellExecute = false; //是否使用作業系統shell啟動 process.StartInfo.CreateNoWindow = false; //是否在新視窗中啟動該程序的值 (不顯示程式視窗) process.Start(); process.WaitForExit(); //等待程式執行完退出程序 process.Close(); } }