gpt4 book ai didi

c# - 安排重新启动的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 15:41:26 32 4
gpt4 key购买 nike

我正在尝试使用 C# 在 winxp 上以编程方式安排重启目前,我将它编程为在 winxp 中添加计划任务以在 5 分钟内重新启动但是,它并不总是有效,因为重启后机器时间可能会更改(我的环境中有时间服务器)。所以这意味着预定任务时间不准确,反过来我的重启没有在预期时间工作。

例如,当我添加定时重启任务时,机器时间为上午 11:05,而重启计划为上午 11:10。然后我将 xp 加入域并重新启动,时间与时间服务器同步,机器更改为下午 1:01。在这种情况下,计划任务的时间是当天上午 11:10,已经过去了。当然不行。

所以有人知道其他方法吗?
ps:这个程序是在xp加入域之前用的并安排重新启动,因为它是第二次重新启动,我需要它在没有用户交互的情况下自动重新启动

最佳答案

使用内置的 shutdown通过从 C# 启动一个新进程来执行命令:

using System;
using System.Diagnostics;

namespace ShutdownApp
{
class ShutdownApp
{
static void Main(string[] args)
{
Process shutDown = new Process();
int shutdownTimeInSeconds = 600; // this will shutdown in 10 minutes, use DateTime and TimeSpan functions to calculate the exact time you need
shutDown.StartInfo.FileName = "shutdown.exe";
shutDown.StartInfo.Arguments = string.Format("-r -t {0}", shutdownTimeInSeconds);

shutDown.Start();
}
}
}

关于c# - 安排重新启动的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8345768/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com