Если на вашем устройстве установлен NTP-клиент, вы можете использовать его. Сервер (ы) настроен в параметре реестра с несколькими строками по адресу:
[HKLM\Services\TIMESVC]
server
Вот код, как заставить клиента синхронизироваться. В противном случае в реестре указывается частота синхронизации клиента (DWORD для мс):
[HKLM\Services\TIMESVC]
Refresh
Принудительная синхронизация:
internal static class NativeMethods
{
internal const int INVALID_HANDLE_VALUE = -1;
internal const int IOCTL_SERVICE_CONTROL = (0x104 << 16) | (7 << 2);
internal const uint GENERIC_READ = 0x80000000;
internal const uint GENERIC_WRITE = 0x40000000;
internal const int OPEN_EXISTING = 3;
[DllImport("coredll.dll", SetLastError = true)]
internal static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
byte[] lpInBuffer,
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr hObject);
}
internal static class TimeServer
{
public static bool SyncTime()
{
byte[] input = System.Text.Encoding.Unicode.GetBytes("Sync\0");
return TimeServer.DeviceIOCTL("NTP0:", NativeMethods.IOCTL_SERVICE_CONTROL, input);
}
private static bool DeviceIOCTL(string deviceName, int ioctl, byte[] input)
{
int size = 0;
return TimeServer.DeviceIOCTL(deviceName, ioctl, input, null, ref size);
}
public static bool DeviceIOCTL(string deviceName, int ioctl, byte[] input, byte[] output, ref int bytesReceived)
{
bool result = false;
IntPtr deviceHandle = NativeMethods.CreateFile(
deviceName,
NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
0,
IntPtr.Zero,
(uint)NativeMethods.OPEN_EXISTING,
0,
IntPtr.Zero);
if (deviceHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE)
{
try
{
result = NativeMethods.DeviceIoControl(
deviceHandle,
ioctl,
input,
(input == null) ? 0 : input.Length,
output,
(output == null) ? 0 : output.Length,
ref bytesReceived,
IntPtr.Zero);
}
finally
{
NativeMethods.CloseHandle(deviceHandle);
}
}
return result;
}
}