1
A
回答
2
如果你的设备上有NTP客户端,你可以使用它。服务器在多串字符串注册表值中配置为:
[HKLM\Services\TIMESVC]
server
以下是代码如何强制客户端进行同步。否则,有客户端同步的频率(MS的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;
}
}
1
我能够使用SNTP Client Project from the Codeproject
进行同步的时间。然而,不得不改变Pinvoke声明使用[DllImport("coredll.dll")]
而不是[DllImport("kernal32.dll")]
我做了这个跑步风移动6.1,紧凑框架3.5
+0
此新版本:http://bocan.ro/Media/Default/Downloads/InternetTime.zip – franDayz 2016-04-11 14:14:07
相关问题
- 1. 从系统中拆分日期和时间::日期时间
- 2. 比较系统日和日期时间
- 3. 从iPhone应用程序中知道设备中的日期和时间设置
- 4. 设置系统日期,同时保持系统时间使用PowerShell
- 5. 从UTC-5时区日期和时间获取iPhone设备的时区日期和时间iPhone应用程序?
- 6. 在VBScript中为日期对象设置日期和时间而不更改系统日期/时间
- 7. Datagridview日期时间格式不使用系统区域设置
- 8. 如何在应用程序中获取android系统日期和时间格式
- 9. 检索系统日期和时间?
- 10. 从mac OS x系统设置中获取日期和时间格式
- 11. 修改设置系统应用程序
- 12. 防止用户系统设置的时间和日期字符串处理
- 13. 如何在.NET应用程序设置系统中以往返格式存储日期时间
- 14. 日期时间选择器 - 设置开始日期和时间
- 15. “自动确定日期和时间”设置开/关程序
- 16. Apache与操作系统的时间差异日期和时间?
- 17. 更改应用程序中设置更改的系统设置
- 18. 如何从角度js应用程序设置系统代理
- 19. 如何同步系统日期/时间?
- 20. 系统日期VS时间戳比较
- 21. 获取当前系统日期时间
- 22. 设置文件的日期和时间
- 23. 如何将默认日期时间设置为mysql中的系统日期时间
- 24. 设置串口通信的应用日期和时间C#
- 25. 开始日期和系统时间之间的差异
- 26. 如何禁用系统更改日期和时间属性
- 27. 如何在Windows WDM驱动程序中设置系统时间?
- 28. 在应用程序中设置iPhone日期/时间用于测试目的?
- 29. 格式化的日期和时间,在“设置 - 日期和时间”
- 30. 如何将系统日期链接到应用程序?
可能重复http://stackoverflow.com/questions/738615/set-device-time-on-net-cf – 2010-05-04 06:14:46
@Sundar:很多没有一度接受答案的问题。开始标记答案或不要惊讶,人们不回答你。 – Shaihi 2010-05-04 06:33:55