我想在C#按钮点击关闭运行exe ..收到错误

时间:2016-07-18 10:22:59

标签: c# .net

我想在C#按钮点击关闭运行exe ..但是我得到了

  

进程已退出,因此无法获取所请求的信息。

我的代码:

Process nynprocess = new Process();
private void button2_Click(object sender, EventArgs e)
{
    string str = @"C:\Program Files\ShareX\ShareX";
    nynprocess.StartInfo.FileName = str;
    nynprocess.Start();
}

private void button3_Click(object sender, EventArgs e)
{
    nynprocess.CloseMainWindow();
    nynprocess.Kill();
    nynprocess.WaitForExit();
}

1 个答案:

答案 0 :(得分:0)

尝试使用此

        // Close process by sending a close message to its main window.
        nynprocess.CloseMainWindow();
        // Free resources associated with process.
        nynprocess.Close();