2013-02-05 84 views
-3

請給我一個工作代碼,用C#.net中的w32tm.exe實現時間同步。我已經嘗試過了。代碼如下所示。如何在C#代碼中使用w32tm重新同步時間?

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); 

但我收到以下錯誤 指定的模塊找不到。 (0x8007007E)。 ,我的要求也希望重定向成功消息的標準輸出。

+0

投票時請加註。由於作者應該知道這個帖子出了什麼問題,如果他是新手。 – 2013-02-05 04:43:22

+0

它可能與此有關:http://support.microsoft.com/kb/978714 –

回答

0

當.Net運行時JITs即將進入的方法時發生錯誤,因爲它無法找到該方法使用的類型之一。

什麼方法,你不能步入做什麼,它使用什麼類型/方法?

請參閱本Link

因此,檢查你試圖加載任何項目是否在文件夾或沒有。

0

您可以嘗試以下C#代碼以啓用來自NTP服務器的日期時間同步。

由我猜這是/ resync命令數量,這樣我就不用發動那個骯髒的外部進程

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

的相關詳細解釋道:

窗口有一個服務,叫做W32Time的,它可以在您的計算機上同步時間,首先我檢查服務正在運行,然後使用ServiceController類,因爲我不知道哪個是resync命令號,以便我可以使用ServiceController啓動命令方法,我使用ProcessStart對該服務啓動一個dos命令:w32tm/resync

+0

你能解釋爲什麼這可以工作嗎?代碼轉儲通常在StackOverflow中被忽略。 – rayryeng

+0

windows有一個名爲w32time的服務,它可以在計算機上同步時間,首先我檢查服務是否正在運行,然後使用ServiceController類,因爲我不知道哪個是resync命令號,以便我可以使用ServiceController啓動命令的方法,我使用ProcessStart啓動該服務的DOS命令:w32tm/resync :) – freakydinde

+0

很酷。用這個信息更新你的文章! – rayryeng

相關問題