我想在设置结束后关闭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()
。
试试这个,这里我是硬编码的小时,分钟和关机值,您可以使用文本框设置这些值。
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");