Retrieve time of remote system

A

Ashutosh

Hi,
How can I retrieve the time of a remote system? Is there some managed
wrapper for this in .Net? I don't mind using P/Invoke also :)

Thanks & Regards,
Ashutosh
 
J

Jeroen Mostert

Ashutosh said:
How can I retrieve the time of a remote system? Is there some managed
wrapper for this in .Net? I don't mind using P/Invoke also :)
You can use 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;
}
}

This code takes care to retrieve the time in UTC and convert it back to a
time local to the machine it's running on, so you don't "see" the remote
machine's time zone bias (if any), but you can also query "Win32_LocalTime"
instead and leave out the conversion if you really want the local time of
the remote machine (which should usually be the same, unless you have a
really big WAN).
 
J

\Ji Zhou [MSFT]\

Hello,

And if you want to use PInvoke to call the native net API, you can use the
NetRemoteTOD function. You can find the NetRemoteTOD document here,
http://msdn.microsoft.com/en-us/library/aa370612(VS.85).aspx

Please let us to know if you have any future questions or concerns on this.
Have a good day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Ashutosh

Thanks Jeroen!

Jeroen said:
You can use 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;
}
}

This code takes care to retrieve the time in UTC and convert it back
to a time local to the machine it's running on, so you don't "see" the
remote machine's time zone bias (if any), but you can also query
"Win32_LocalTime" instead and leave out the conversion if you really
want the local time of the remote machine (which should usually be the
same, unless you have a really big WAN).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top