3

.NETでシステム時刻を正しいサーバー時刻に設定することは可能ですか?システム時刻が混乱するテストケースがあります。テストケースの分解で復元したいと思います。プログラムで(サーバーなどから)正しい時刻を復元することは可能ですか?

注:単体テストと受け入れテストの両方でこれが必要になります。偽の時間を注入することは後者には適用されません。

4

2 に答える 2

3

これは時間を設定することです(あなたがそれを知ったら):

プログラムでシステム日付を変更する

有効なソースから時間を取得する場合は、おそらくこのディスカッションの回答が役立ちます 。C#を使用してNTPサーバーをクエリする方法は?

于 2012-11-29T10:43:28.517 に答える
2
 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();
        }
    }
于 2015-08-12T14:16:10.737 に答える