另一台机器的时间

在c#中我们使用DateTime.Now属性值是本地机器的当前日期和时间如何获取具有IP地址或机器名称的另一台机器的时间

您可以通过编写提供当前时间的服务来实现目标吗? 或连接到远程机器并发送一些wmi查询

类似的问题: http : //social.msdn.microsoft.com/forums/en-US/netfxremoting/thread/f2ff8a33-df5d-4bad-aa89-7b2a2dd73d73/

没有内置的方法来做到这一点。 您将不得不要求机器通过某种通信协议告诉您它的时间。 例如,您可以创建一个WCF服务以在另一台计算机上运行并公开服务协定以返回系统时间。 请记住,由于网络跳跃会有一些延迟,因此您返回的时间将是过时的几毫秒(或秒,具体取决于连接速度)。

如果您想要快速而肮脏的方式来执行此操作,而不需要.NET或在其他计算机上运行任何特殊操作,则可以使用PSExec 。

你可以在没有WMI的情况下通过C#获得它

using System; using System.Collections.Generic; using System.Diagnostics; namespace RemoteSystemTime { class Program { static void Main(string[] args) { try { string machineName = "vista-pc"; Process proc = new Process(); proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.FileName = "net"; proc.StartInfo.Arguments = @"time \\" + machineName; proc.Start(); proc.WaitForExit(); List results = new List(); while (!proc.StandardOutput.EndOfStream) { string currentline = proc.StandardOutput.ReadLine(); if (!string.IsNullOrEmpty(currentline)) { results.Add(currentline); } } string currentTime = string.Empty; if (results.Count > 0 && results[0].ToLower().StartsWith(@"current time at \\" + machineName.ToLower() + " is ")) { currentTime = results[0].Substring((@"current time at \\" + machineName.ToLower() + " is ").Length); Console.WriteLine(DateTime.Parse(currentTime)); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } } }