获取远程PC的日期时间?

时间:2010-05-14 18:57:28

标签: .net datetime networking system

是否有任何课程可以在.net中获取远程PC的日期时间?为了做到这一点,我可以使用计算机名称或时区。对于每种情况,有不同的方法来获取当前日期时间?我正在使用Visual Studio 2005。

4 个答案:

答案 0 :(得分:5)

我给你一个使用WMI的解决方案。您可能需要也可能不需要域名和安全信息:

try
{
    string pc = "pcname";
    //string domain = "yourdomain";
    //ConnectionOptions connection = new ConnectionOptions();
    //connection.Username = some username;
    //connection.Password = somepassword;
    //connection.Authority = "ntlmdomain:" + domain;

    string wmipath = string.Format("\\\\{0}\\root\\CIMV2", pc);
    //ManagementScope scope = new ManagementScope(
    //    string.Format("\\\\{0}\\root\\CIMV2", pc), connection);
    ManagementScope scope = new ManagementScope(wmipath);
    scope.Connect();

    ObjectQuery query = new ObjectQuery(
        "SELECT * FROM Win32_LocalTime");

    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(scope, query);

    foreach (ManagementObject queryObj in searcher.Get())
    {
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("Win32_LocalTime instance");
        Console.WriteLine("-----------------------------------");

        Console.WriteLine("Date: {0}-{1}-{2}", queryObj["Year"], queryObj["Month"], queryObj["Day"]);
        Console.WriteLine("Time: {0}:{1}:{2}", queryObj["Hour"], queryObj["Minute"], queryObj["Second"]);
    }
}
catch (ManagementException err)
{
    Console.WriteLine("An error occurred while querying for WMI data: " + err.Message);
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
    Console.WriteLine("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}

答案 1 :(得分:1)

您可以使用远程WMI和Win32_TimeZone class。您需要具有在该计算机上执行WMI查询的权限。通过使用UTC而不是当地时间避免这样的麻烦。

答案 2 :(得分:1)

在Windows计算机上有net time \\<remote-ip address>来获取远程计算机的时间。您可以使用它来查找远程系统时间,如果您想要代码,可以将其包装在函数中以在C#中执行shell(DOS)函数。

答案 3 :(得分:1)

由于WMI代码非常慢,您可以使用以下代码来获得更快的结果

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