当前位置: 首页 > 编程语言 > C#

另一台机器的时间共享

时间:2023-04-10 15:47:20 C#

另一台机器的时间在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#获取它。以上就是《C#学习教程:另一台机器的时间共享》的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingSystem;使用System.Collections.Generic;使用系统诊断;namespaceRemoteSystemTime{classProgram{staticvoidMain(string[]args){try{stringmachineName="vista-pc";Processproc=newProcess();proc.StartInfo.UseShellExecute=false;proc.StartInfo.RedirectStandardOutput=true;proc.StartInfo.FileName="网络";proc.StartInfo.Arguments=@"time\"+machineName;proc.开始();proc.WaitForExit();列表结果=newList();while(!proc.StandardOutput.EndOfStream){stringcurrentline=proc.StandardOutput.ReadLine();如果(!string.IsNullOrEmpty(currentline)){results.Add(currentline);}}stringcurrentTime=string.Empty;if(results.Count>0&&results[0].ToLower().StartsWith(@"currenttimeat\"+machineName.ToLower()+"is")){currentTime=results[0].Substring((@"当前时间在\"+machineName.ToLower()+"是").L长度);Console.WriteLine(DateTime.Parse(currentTime));控制台.ReadLine();}}catch(Exceptionex){Console.WriteLine(ex.Message);控制台.ReadLine();}}}}网络收藏不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: