views:

328

answers:

4

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:

http://stackoverflow.com/questions/204936/set-time-programmatically-using-c

Thanks

A: 

You can do this using the command line tool w32tm:

w32tm /resync

This is neither C# nor .NET, but it hopefully fits your requirements.

PVitt
+3  A: 

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();
    }
}
Justin Grant
+1. This is exactly what I was writing up, you just beat me to it. Nice one.
Simon P Stevens
A: 

The easiest way would be to use System.Diagnostics.Process to run a net time command with the appropriate parameters to sync back to your DC.

Run this from a command prompt to see what I'm talking about:

net time /?
ParmesanCodice
Soory for the double post, Justin beat me to it.
ParmesanCodice
A: 

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.

sleske
That was definately my first option, to do something like in the following posthttp://stackoverflow.com/questions/43711/whats-a-good-way-to-overwrite-datetime-now-during-testingUnfortunately, I am working with a platform solution that does not give me access to doing such a thing. It's either this, or not do regression testing, and I don't like the alternative.
helios456