次の C# コードを試して、NTP サーバーからの日時同期を有効にすることができます。
ちなみに、/resync コマンド番号はどれだと思いますので、その汚い外部プロセスを起動する必要はありません
/// <summary>Synchronizes the date time to ntp server using w32time service</summary>
/// <returns><c>true</c> if [command succeed]; otherwise, <c>false</c>.</returns>
public static bool SyncDateTime()
{
try
{
ServiceController serviceController = new ServiceController("w32time");
if (serviceController.Status != ServiceControllerStatus.Running)
{
serviceController.Start();
}
Logger.TraceInformation("w32time service is running");
Process processTime = new Process();
processTime.StartInfo.FileName = "w32tm";
processTime.StartInfo.Arguments = "/resync";
processTime.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processTime.Start();
processTime.WaitForExit();
Logger.TraceInformation("w32time service has sync local dateTime from NTP server");
return true;
}
catch (Exception exception)
{
Logger.LogError("unable to sync date time from NTP server", exception);
return false;
}
}
詳細な説明:
Windowsにはw32timeと呼ばれるサービスがあり、コンピューターの時間を同期できます。まず、ServiceControllerクラスを使用してサービスが実行されていることを確認します。次に、どれが再同期コマンド番号かわからないため、ServiceControllerの起動を使用できますコマンド メソッド、私は ProcessStart を使用して、そのサービスで dos コマンドを起動します: w32tm /resync