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

问题描述 投票:-3回答:1

我想在设置结束后关闭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()

c# windows winforms shutdown system-shutdown
1个回答
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");

© www.soinside.com 2019 - 2024. All rights reserved.