Unity中調用某個程序運行

在類中引用 using System.Runtime.InteropServices;

第一種格式: System.Diagnostics.Process p = new System.Diagnostics.Process();
         p.StartInfo.FileName = "osk.exe";
         p.Start();

第二種格式:System.Diagnostics.Process.Start("C:/Users/fun2-dev3/Desktop/G.exe");

退出程序:

方法一:這種方法會阻塞當前進程,直到運行的外部程序退出
System.Diagnostics.Process exep = System.Diagnostics.Process.Start(@"C:\Windows\Notepad.exe");
exep.WaitForExit();//關鍵,等待外部程序退出後才能往下執行
MessageBox.Show("Notepad.exe運行完畢");

方法二:爲外部進程添加一個事件監視器,當退出後,獲取通知,這種方法時不會阻塞當前進程,你可以處理其它事情
System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = @"C:\Windows\Notepad.exe";
exep.EnableRaisingEvents = true;
exep.Exited += new EventHandler(exep_Exited);
exep.Start();

//exep_Exited事件處理代碼,這裏外部程序退出後激活,可以執行你要的操作
void exep_Exited(object sender, EventArgs e)
{
            MessageBox.Show("Notepad.exe運行完畢");
}

1、CloseMainWindow()

通過向進程的主窗口發送關閉的消息來關閉擁有用戶界面的進程。

2、kill()

終止沒有圖形化界面的進程的唯一方法

public void ExeClassButton(string exeName)
    {
        exeClass = new Process();
        steamVR = new Process();
        exeClass.StartInfo.FileName = Application.streamingAssetsPath + "/Exes/"+exeName;
        steamVR.StartInfo.FileName = Application.streamingAssetsPath+ "/Exes/SteamVR/bin/win32/vrmonitor.exe";
        exeClass.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        exeClass.EnableRaisingEvents = true;
        exeClass.Exited += new EventHandler(ExeClassExited);
        steamVR.Start();
        exeClass.Start();
    }


void ExeClassExited(object sender, EventArgs e)
    {
        steamVR.Kill();
        //steamVR.CloseMainWindow();
        ShowWindow(menuHandle, 4);
    }

 

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