-1

Please give me a working code for achieving the time synchronization using w32tm.exe in C#.net. I already tried. Code shown below.

    System.Diagnostics.Process p;
    string output;
    p = new System.Diagnostics.Process();
    p.StartInfo = procStartInfo;
    p.StartInfo.FileName = "w32tm";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;

    p.StartInfo.Arguments = " /resync /computer:xxxxx977";
    p.Start();
    p.WaitForExit();

    output = p.StandardOutput.ReadLine().ToString();
    MessageBox.Show(output);

But i am getting the following error The specified module could not be found. (0x8007007E). and my requirement also wants to redirect the standardoutput for the success message.

Karthik Sampath
  • 101
  • 2
  • 7

2 Answers2

2

you can try following C# code to enable date time sync from NTP server.

by the way i guess which is /resync command number so that i wouldn't have to launch that dirty external process

/// <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;
    }
}

detailled explanation :

windows have a service, called w32time, which can sync time on your computer, first i check that the service is running, using ServiceController class, then, because i don't know which is the resync command number so that i can use ServiceController launch command method, i use a ProcessStart to launch a dos command on that services : w32tm /resync

freakydinde
  • 1,070
  • 1
  • 9
  • 10
  • Can you explain why this works? Code dumping is usually frowned upon here at StackOverflow. – rayryeng Dec 16 '14 at 16:55
  • windows have a service, called w32time, which can sync time on your computer, first i check that the service is running, using ServiceController class, then, because i don't know which is the resync command number so that i can use ServiceController launch command method, i use a ProcessStart to launch a dos command on that services : w32tm /resync :) – freakydinde Dec 17 '14 at 09:39
  • In what .net is this working? I need this for XP and .NET4. There is no namespace `ServiceController`? – Hrvoje T Sep 25 '18 at 09:11
  • Ok, I found the answer here. https://stackoverflow.com/questions/19763527/why-my-c-sharp-does-not-have-system-serviceprocess-library . `System.ServiceProcess` namespace belongs on `System.ServiceProcess.dll` and it doesn't added as a reference by default. You have to go to `Solution Explorer` and right click on `References` and add `System.ServiceProcess` – Hrvoje T Sep 25 '18 at 09:20
  • Found `Logger` in `Microsoft.Build.Utilities` but it doesn't have `TraceInformation()` method. – Hrvoje T Sep 25 '18 at 10:00
0

The error is occurring when the .Net runtime JITs the method you're about to step into, because it couldn't find one of the types used by the method.

What exactly does the method that you can't step into do, and what types / methods does it use?

Refer this Link

So check whether any item you tried to load is in the folder or not.

Community
  • 1
  • 1