使用WMI获取远程机器的时间
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;
}
}
浙公网安备 33010602011771号