.NETでシステム時刻を正しいサーバー時刻に設定することは可能ですか?システム時刻が混乱するテストケースがあります。テストケースの分解で復元したいと思います。プログラムで(サーバーなどから)正しい時刻を復元することは可能ですか?
注:単体テストと受け入れテストの両方でこれが必要になります。偽の時間を注入することは後者には適用されません。
.NETでシステム時刻を正しいサーバー時刻に設定することは可能ですか?システム時刻が混乱するテストケースがあります。テストケースの分解で復元したいと思います。プログラムで(サーバーなどから)正しい時刻を復元することは可能ですか?
注:単体テストと受け入れテストの両方でこれが必要になります。偽の時間を注入することは後者には適用されません。
これは時間を設定することです(あなたがそれを知ったら):
有効なソースから時間を取得する場合は、おそらくこのディスカッションの回答が役立ちます 。C#を使用してNTPサーバーをクエリする方法は?
void setDate(string dateInYourSystemFormat)
{
var proc = new System.Diagnostics.ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = @"C:\Windows\System32";
proc.CreateNoWindow = true;
proc.FileName = @"C:\Windows\System32\cmd.exe";
proc.Verb = "runas";
proc.Arguments = "/C date " + dateInYourSystemFormat;
try
{
System.Diagnostics.Process.Start(proc);
}
catch
{
MessageBox.Show("Error to change time of your system");
Application.ExitThread();
}
}
void setTime(string timeInYourSystemFormat)
{
var proc = new System.Diagnostics.ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = @"C:\Windows\System32";
proc.CreateNoWindow = true;
proc.FileName = @"C:\Windows\System32\cmd.exe";
proc.Verb = "runas";
proc.Arguments = "/C time " + timeInYourSystemFormat;
try
{
System.Diagnostics.Process.Start(proc);
}
catch
{
MessageBox.Show("Error to change time of your system");
Application.ExitThread();
}
}