Get remote PC's date time?

后端 未结 4 1292
误落风尘
误落风尘 2021-01-15 00:12

Is there any class available to get a remote PC\'s date time in .net? In order to do it, I can use a computer name or time zone. For each case, are there different ways to g

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 00:39

    Since WMI code would be very slow, you can use the below code to get faster results

                string machineName = "vista-pc";
    
                System.Diagnostics.Process proc = new System.Diagnostics.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();
                }
    

提交回复
热议问题