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);
    }

 

你可能感兴趣的:(Unity)