I have time based tests to run that require changing the system time multiple times during the test. I want to be able to resync the time to the domain controller time at the end of the test. I there any way to do that using .NET code (C#). I am changing the time using the p-invoke function found in:
Set time programmatically using C#
Thanks
One easy way would be to launch a process and run the NET TIME command (copied from http://blogs.msdn.com/sanket/archive/2006/11/02/synchronizing-machine-time-with-domain-controller.aspx)
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process netTime = new Process();
netTime.StartInfo.FileName = "NET.exe";
netTime.StartInfo.Arguments = "TIME /domain:mydomainname /SET /Y";
netTime.Start();
}
}
As an addition to the other answers:
You should seriously consider making the notion of "current time" somehow injectable into your system. Directly reading from the system clock is very problematic (even when running the app), precisely because it is global state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With