VB.net 2005获取远程计算机的系统时间?

发布于 2024-07-19 05:28:58 字数 136 浏览 3 评论 0原文

我正在尝试设置一个“AT”作业来导出远程计算机上的一些注册表项,问题是 DOS 命令需要时间来运行。 我想获取远程计算机的系统时间,以便我可以安排它从我发送命令后运行 1 分钟。

有没有办法用VB.Net代码获取远程计算机的系统时间?

I am trying to set up an "AT" job to export some registry keys on a remote computer, the problem is that DOS command requires a time to run. I want to get the system time of the remote computer so i can schedule it to run 1 minute from the time i send the command.

Is there any way to get the system time of a remote computer with VB.Net code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-07-26 05:28:58

这就是我要做的工作,感谢你的帮助 Jon B.

    Dim p As New System.Diagnostics.Process
    Dim pinfo As New System.Diagnostics.ProcessStartInfo
    Dim pout As String
    pinfo.FileName = ("C:\WINDOWS\system32\net.exe")
    pinfo.Arguments = ("time \\computername")
    pinfo.RedirectStandardOutput = True
    pinfo.UseShellExecute = False
    pinfo.CreateNoWindow = True
    p = Diagnostics.Process.Start(pinfo)
    p.WaitForExit()
    pout = p.StandardOutput.ReadLine
    MsgBox(pout)

here is what i got to work, thanks for all your help Jon B.

    Dim p As New System.Diagnostics.Process
    Dim pinfo As New System.Diagnostics.ProcessStartInfo
    Dim pout As String
    pinfo.FileName = ("C:\WINDOWS\system32\net.exe")
    pinfo.Arguments = ("time \\computername")
    pinfo.RedirectStandardOutput = True
    pinfo.UseShellExecute = False
    pinfo.CreateNoWindow = True
    p = Diagnostics.Process.Start(pinfo)
    p.WaitForExit()
    pout = p.StandardOutput.ReadLine
    MsgBox(pout)
沉溺在你眼里的海 2024-07-26 05:28:58

您可以使用:

net time \\computer_name

您只需要对此进行处理并解析结果。


这是一些示例代码。 它是用 C# 编写的,但应该很容易翻译。

    static DateTime GetRemoteDateTime(string machineName)
    {
        machineName = @"\\" + machineName;
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("net", "time " + machineName);
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo);
        p.WaitForExit();

        string output = p.StandardOutput.ReadLine();
        output = output.Replace("Current time at " + machineName + " is ", "");
        return DateTime.Parse(output);
    }

我没有费心添加任何错误处理(如果找不到机器等) - 您可以添加任何您需要的内容来满足您的目的。

You can use:

net time \\computer_name

You'll just need to shell out to this and parse the result.


Here's some sample code. It's in C#, but should be pretty easy to translate.

    static DateTime GetRemoteDateTime(string machineName)
    {
        machineName = @"\\" + machineName;
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("net", "time " + machineName);
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo);
        p.WaitForExit();

        string output = p.StandardOutput.ReadLine();
        output = output.Replace("Current time at " + machineName + " is ", "");
        return DateTime.Parse(output);
    }

I didn't bother adding any error handling (if the machine is not found, etc) - you can add in whatever you need to suit your purposes.

够钟 2024-07-26 05:28:58

..如果你使用的话,也许会做一点检查

    ...
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    ...

    string shellOut = "";
    if (p.ExitCode == 0)
    {
        shellOut = p.StandardOutput.ReadToEnd();
        Console.WriteLine("Operation completed successfully.");
    }
    else
    {
        shellOut = p.StandardError.ReadToEnd();
        Console.WriteLine(shellOut);
    }

..and maby do a little check if you use

    ...
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    ...

    string shellOut = "";
    if (p.ExitCode == 0)
    {
        shellOut = p.StandardOutput.ReadToEnd();
        Console.WriteLine("Operation completed successfully.");
    }
    else
    {
        shellOut = p.StandardError.ReadToEnd();
        Console.WriteLine(shellOut);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文