none
Get another system time with IP in C # RRS feed

  • Question

  • Hi How i get time from another system with IP at network wlan in C # winform ?thanks.


    • Edited by Ali_Lonbar Monday, October 28, 2019 10:56 AM
    Monday, October 28, 2019 7:35 AM

All replies

  • Hi,

    Your question is very short, so I'm not sure if I understand it correctly.

    Could the following code help you?

    static void Main(string[] args)
    {
        try
        {
            string machineName = "machine-name-of-other-pc";
            Process proc = new Process
            {
                StartInfo =
                {
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    FileName = "net",
                    Arguments = @"time \\" + machineName
                }
            };
            proc.Start();
            proc.WaitForExit();
    
            while (!proc.StandardOutput.EndOfStream)
            {
                string output = proc.StandardOutput.ReadLine();
    
                if (!string.IsNullOrEmpty(output))
                {
                    Console.WriteLine(output);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    
    
        Console.ReadLine();
    }

    Kind regards,

    Johnny Hooyberghs

    Monday, October 28, 2019 12:45 PM
  • My problem was not solved. This code only works if you connect to the system with administrative access
    Monday, October 28, 2019 12:56 PM
  • Hi,

    In that case you will need to create two applications.

    One application that runs on the other system and your WinForms.

    These two applications need to be able to communicate using some kind of available technology like WCF, ASP.NET WebApi, TCP sockets or gRCP (which is availble in .NET Core 3).

    Kind regards,

    Johnny Hooyberghs

    Monday, October 28, 2019 1:10 PM
  • If you want to get the system time of a remote computer (?), the NetRemoteTOD function could be useful for you. On that site on Pinvoke.net you'll find a working implementation of this method. I spare us to copy and paste the complete code, but I tested it and it worked "like a charm".

    Mind, even if there is a using directive for System.Windows.Forms, it is a Console application. This directive appears to be useless.

    wizend

    Monday, October 28, 2019 1:10 PM
  • Hi Ali,

    Thank you for posting here.

    For your question, you want to get time from another system with IP at network wlan.

    You can use WMI to do this. 

    It should be noted that WMI needs to be set in the firewall.

    Here is my code:

            static void Main(string[] args)
            {
                string ComputerName = GetMachineNameFromIPAddress("ip");
                string DomainName = "";
                string UserName = "";
                string Password = "";
                try
                {
                    ConnectionOptions co = new ConnectionOptions();
    
                    co.Username = DomainName + @"\" + UserName;
                    co.Password = Password;
    
                    co.EnablePrivileges = true;
                    ManagementScope ms = new ManagementScope(@"\\" + ComputerName + @"\root\cimv2", co);
                    ObjectQuery oq = new ObjectQuery("select * from win32_localtime");
                    ManagementObjectSearcher os = new ManagementObjectSearcher(ms, oq);
                    ManagementObjectCollection cq = os.Get();
    
                    foreach (ManagementObject mo in cq)
                    {
                        Console.WriteLine("{0}:{1}:{2} {3}/{4}/{5}", mo["Hour"], mo["Minute"],mo["Second"],mo["Day"],mo["Month"],mo["Year"]);
                    }
    
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            private static string GetMachineNameFromIPAddress(string ipAdress)
            {
                string machineName = string.Empty;
                try
                {
                    IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
    
                    machineName = hostEntry.HostName;
                }
                catch (Exception ex)
                {
                    // Machine not found...
                }
                return machineName;
            }
    

    Hope my solution could be helpful.

    Best regards,
    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 29, 2019 9:47 AM
  • Can you get more help?. Do you have a sample of code on this?
    • Edited by Ali_Lonbar Wednesday, October 30, 2019 6:29 AM
    Wednesday, October 30, 2019 6:29 AM
  • Hi Ali,

    Thanks for your feedback.

    I tested the above code, it can get the machine name by IP and get the time on another machine.

    So I am not sure what you mean of " Do you have a sample of code on this".

    Could you explain more in detail?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 30, 2019 8:58 AM
  • Can you get more help?. Do you have a sample of code on this?
    Wednesday, October 30, 2019 10:05 AM
  • Did you try the NetRemoteTOD class I showed you? Following the link in my earlier post you'll find a complete working example.

    wizend

    Wednesday, October 30, 2019 10:24 AM