获取远程服务器的准确时间

Dav*_*ing 5 .net c# time

在C#中,如何查询远程服务器的当前时间?

类似的功能

net time \\servername
Run Code Online (Sandbox Code Playgroud)

但返回包含秒的日期戳.

谢谢

Pat*_*ald 5

您可以使用NetRemoteTOD功能.

来自http://bytes.com/groups/net-c/246234-netremotetod-usage的示例:

// The pointer.
IntPtr pintBuffer = IntPtr.Zero;

// Get the time of day.
int pintError = NetRemoteTOD(@"\\sony_laptop", ref pintBuffer);

// Get the structure.
TIME_OF_DAY_INFO pobjInfo = (TIME_OF_DAY_INFO)
Marshal.PtrToStructure(pintBuffer, typeof(TIME_OF_DAY_INFO));

// Free the buffer.
NetApiBufferFree(pintBuffer);
Run Code Online (Sandbox Code Playgroud)


Ree*_*sey 1

Windows 时间服务实现 NTP。这是NTP 客户端的 C# 实现使用它的 Windows GUI 可以在Simple Network Time Protocol Client中找到。这是瓦勒·博坎的作品。