使用WMI获取远程机器的时间

using System.Management;

   public static DateTime? GetSystemTime(string computerName) {
     using (
       ManagementObjectSearcher searcher = new ManagementObjectSearcher(
         string.Format(@"\\{0}\root\cimv2", computerName),
         "SELECT * FROM Win32_UTCTime"
       )
     ) {
       foreach (ManagementObject time in searcher.Get()) {
         int year = Convert.ToInt32(time.GetPropertyValue("Year"));
         int month = Convert.ToInt32(time.GetPropertyValue("Month"));
         int day = Convert.ToInt32(time.GetPropertyValue("Day"));
         int hour = Convert.ToInt32(time.GetPropertyValue("Hour"));
         int minute = Convert.ToInt32(time.GetPropertyValue("Minute"));
         int second = Convert.ToInt32(time.GetPropertyValue("Second"));
         return new DateTime(year, month, day, hour, minute, second,
DateTimeKind.Utc).ToLocalTime();
       }
       return null;
     }
   }

posted @ 2009-08-19 23:09  Jack.Lee  阅读(484)  评论(0)    收藏  举报