-1

C#.net で w32tm.exe を使用して時刻同期を実現するための実用的なコードを教えてください。私はすでに試しました。以下に示すコード。

    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)。私の要件では、成功メッセージの標準出力もリダイレクトしたいと考えています。

4

2 に答える 2

1

次の 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;
    }
}

詳細な説明:

Windowsにはw32timeと呼ばれるサービスがあり、コンピューターの時間を同期できます。まず、ServiceControllerクラスを使用してサービスが実行されていることを確認します。次に、どれが再同期コマンド番号かわからないため、ServiceControllerの起動を使用できますコマンド メソッド、私は ProcessStart を使用して、そのサービスで dos コマンドを起動します: w32tm /resync

于 2014-12-16T16:35:31.500 に答える
0

このエラーは、.NetランタイムがステップインしようとしているメソッドをJITするときに発生します。これは、メソッドで使用されているタイプの1つが見つからなかったためです。

あなたが踏み込むことができない方法は正確に何をしますか、そしてそれはどのタイプ/方法を使用しますか?

このリンクを参照してください

したがって、ロードしようとしたアイテムがフォルダ内にあるかどうかを確認してください。

于 2013-02-05T04:36:26.770 に答える