PC立即关闭process.start(“ shutdown”,“ / s / t”)

时间:2018-07-01 16:40:09

标签: c# windows winforms shutdown system-shutdown

我要在设置的时间结束后关闭PC。(对于输入,我使用了3个TextBoxes),但是当我按下按钮时:

private void button1_Click(object sender, EventArgs e)
{
    int totalSeconds = int.Parse(hours.Text.ToString()) * 120 +
                       int.Parse(minutes.Text.ToString()) * 60 + 
                       int.Parse(seconds.Text.ToString());

    System.Diagnostics.Process.Start("shutdown", "/s /t " + totalSeconds.ToString());
}

PC立即关闭。

编辑:上面的代码不是问题,它工作得很好,问题是我在InitialiseComponent();函数中有两次Form1()

1 个答案:

答案 0 :(得分:0)

尝试一下,这里我对小时,分钟和关机值进行了硬编码,您可以使用文本框设置这些值。

private void button1_Click(object sender, EventArgs e) {

 string shutdown = "-s"; // -s -> shutdown
 int hour = 1 * 60 * 60; //3600 seconds
 int minute = 1 * 60; //60 seconds
 int second = 1; //1 seconds
 int totalSeconds = hour + minute + second; //total seconds should be : 3661
 Process.Start("Shutdown", shutdown + " -t " + totalSeconds + @" -c ""Windows is going to shutdown!"""); // -t = time , -c = comments

}

此外,如果要中止关机,可以使用以下代码行Process.Start("Shutdown", "-a");