C#运行一个exe并等待它关闭

时间:2012-09-05 11:17:18

标签: c# windows

希望您可以帮助我使用下面的代码,当用户登录某台PC时,下面的程序会运行。运行后exe程序关闭我希望PC注销,下面的myProcess_Exited方法没有运行,你能发现任何问题吗?

由于 史蒂芬

private void myProcess_Exited(object sender, System.EventArgs e)
 {
         System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
         proc.StartInfo.FileName = "C:\\windows\\SysWOW64\\shutdown.exe";
         proc1.StartInfo.Arguments = "/l";
         proc1.StartInfo.UseShellExecute = false;
         proc1.StartInfo.RedirectStandardOutput = false;
         proc1.Start();
         Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)
{
             System.Diagnostics.ProcessStartInfo p = new                                            System.Diagnostics.ProcessStartInfo(@"K:\App\pc\stub.exe");
             p.Arguments = "-RunForever";
             proc = new System.Diagnostics.Process();
             proc.StartInfo = p;
             proc.EnableRaisingEvents = true;
             proc.Exited += new EventHandler(myProcess_Exited);
             proc.Start();
 }
}

1 个答案:

答案 0 :(得分:0)

下面的一个非常好。

    Process proc = new Process();

    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
        proc1.StartInfo.FileName = "C:\\windows\\SysWOW64\\shutdown.exe";
        proc1.StartInfo.Arguments = "/l";
        proc1.StartInfo.UseShellExecute = false;
        proc1.StartInfo.RedirectStandardOutput = false;
        proc1.Start();
        Application.Exit();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"K:\App\pc\stub.exe");
        p.Arguments = "-RunForever";
        Process proc = new System.Diagnostics.Process();
        proc.StartInfo = p;
        proc.StartInfo.CreateNoWindow = true;
        proc.EnableRaisingEvents = true;
        proc.Exited += new EventHandler(myProcess_Exited);
        proc.Start();
    }