Public Function GetNetworkTime() As DateTime Dim ep As New Net.IPEndPoint(Dns.GetHostEntry("time-a.nist.gov").AddressList(0), 123) Dim s As New Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Dgram, Sockets.ProtocolType.Udp) s.Connect(ep) Dim ntpData As Byte() = New Byte(47) {} ' RFC 2030' ntpData(0) = &H1B For i As Integer = 1 To 47 ntpData(i) = 0 Next s.Send(ntpData) s.Receive(ntpData) Dim offsetTransmitTime As Byte = 40 Dim intpart As ULong = 0 Dim fractpart As ULong = 0 For i As Integer = 0 To 3 intpart = CULng(256 * intpart + ntpData(offsetTransmitTime + i)) Next For i As Integer = 4 To 7 fractpart = CULng(256 * fractpart + ntpData(offsetTransmitTime + i)) Next Dim milliseconds As ULong = CULng((intpart * 1000 + (fractpart * 1000) / &H100000000L)) s.Close() Dim timeSpan__1 As TimeSpan = TimeSpan.FromTicks(CLng(milliseconds) * TimeSpan.TicksPerMillisecond) Dim dateTime As New DateTime(1900, 1, 1) dateTime += timeSpan__1 Dim offsetAmount As TimeSpan = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime) Dim networkDateTime As DateTime = (dateTime + offsetAmount) Debug.Print(networkDateTime.ToString()) Return networkDateTime End Function
These lines are throing errors:
intpart = 256 * intpart + ntpData(offsetTransmitTime + i)
fractpart = CULng(256 * fractpart + ntpData(offsetTransmitTime + i))
Dim milliseconds As ULong = CULng((intpart * 1000 + (fractpart * 1000) / &H100000000L))
I get these errors:
Requested operation is not available because the runtime library function 'System.Convert.ToUInt64' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Add' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Divide' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Multiply' is not defined.
The exact same code works without problem in a regular .NET Framework (console) application.