如何使用C#从互联网或服务器获取当前日期和时间?我想时间如下:
public static DateTime GetNetworkTime (string ntpServer)
{
IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;
if (address == null || address.Length == 0)
throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");
IPEndPoint ep = new IPEndPoint(address[0], 123);
return GetNetworkTime(ep);
}
Run Code Online (Sandbox Code Playgroud)
我正在传递服务器IP地址netServer,但它无法正常工作.