Another car time

In C #, when we use DateTime.Now, the property value is the current date and time of the local machine, how can I get the time on another machine with the IP address or machine name

+3
source share
3 answers

Can you achieve by writing a service that gives you the current time? or connecting to a remote computer and sending some wmi request

Similar question: http://social.msdn.microsoft.com/forums/en-US/netfxremoting/thread/f2ff8a33-df5d-4bad-aa89-7b2a2dd73d73/

+3
source

. - . , WCF , . , - - , , , ( ) .

, .NET - , PSExec.

0

You can get it using C # without WMI

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<string> results = new List<string>();

                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();
            }
        }
    }
}
0
source

All Articles