VB.NET2005获取远程计算机的系统时间?
我正在尝试设置一个“AT”作业来导出远程计算机上的一些注册表项,问题是DOS命令需要一段时间才能运行。我想获取远程计算机的系统时间,这样我就可以安排它从发送命令开始运行1分钟 有没有办法用VB.Net代码获取远程计算机的系统时间?您可以使用:VB.NET2005获取远程计算机的系统时间?,vb.net,system-information,Vb.net,System Information,我正在尝试设置一个“AT”作业来导出远程计算机上的一些注册表项,问题是DOS命令需要一段时间才能运行。我想获取远程计算机的系统时间,这样我就可以安排它从发送命令开始运行1分钟 有没有办法用VB.Net代码获取远程计算机的系统时间?您可以使用: net time \\computer_name 您只需要对此进行详细分析并解析结果 下面是一些示例代码。它是C语言的,但应该很容易翻译 static DateTime GetRemoteDateTime(string machineName)
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);
}
我没有添加任何错误处理(如果找不到机器等)-您可以添加任何您需要的内容以满足您的目的。您可以使用:
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);
}
我没有添加任何错误处理(如果找不到机器等)-您可以添加任何您需要的内容以满足您的目的。以下是我的工作内容,感谢您的帮助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)
这是我的工作,谢谢你的帮助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)
…如果你使用
...
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);
}
…如果你使用
...
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);
}
谢谢你能给我一个如何解析结果的例子吗,我不知道该怎么做。获取StandardOutput并出错。Readline StandardOut尚未重定向或进程尚未启动。知道那是什么意思吗?如果你需要查看,我会发布我的代码。再次感谢你的帮助!为我工作。。。将RedirectStandardOutput设置为True非常重要。谢谢!你能给我一个如何解析结果的例子吗,我不知道该怎么做。获取StandardOutput并出错。Readline StandardOut尚未重定向或进程尚未启动。知道那是什么意思吗?如果你需要查看,我会发布我的代码。再次感谢你的帮助!为我工作。。。将RedirectStandardOutput设置为True非常关键。我在答案中添加了一个代码示例。它是C#,但应该很容易转换为VBI。在我的答案中添加了一个代码示例。它是C#,但应该很容易转换为VB