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. 是否可以從我的Android應用程序設置系統日期時間?
- 2. Node.js - 設置系統日期/時間
- 3. 從系統中拆分日期和時間::日期時間
- 4. 在linux中的C++程序中設置系統日期和時間
- 5. 比較系統日和日期時間
- 6. 從iPhone應用程序中知道設備中的日期和時間設置
- 7. 設置系統時間和日期iOS Swift
- 8. 設置系統日期,同時保持系統時間使用PowerShell
- 9. 從UTC-5時區日期和時間獲取iPhone設備的時區日期和時間iPhone應用程序?
- 10. 在VBScript中爲日期對象設置日期和時間而不更改系統日期/時間
- 11. Datagridview日期時間格式不使用系統區域設置
- 12. 如何在應用程序中獲取android系統日期和時間格式
- 13. 檢索系統日期和時間?
- 14. 當前系統日期和時間
- 15. 從mac OS x系統設置中獲取日期和時間格式
- 16. 修改設置系統應用程序
- 17. Windows 7批量設置系統日期/時間
- 18. 防止用戶系統設置的時間和日期字符串處理
- 19. 使用蘋果筆的閱讀日期和時間系統偏好設置
- 20. 如何從我的android應用程序調用日期時間設置屏幕?
- 21. 如何在.NET應用程序設置系統中以往返格式存儲日期時間
- 22. 日期時間選擇器 - 設置開始日期和時間
- 23. 設置日期時間爲UTC,而不是把它寫當前系統時間
- 24. 編程,應用程序和操作系統的時間精度
- 25. 「自動確定日期和時間」設置開/關程序
- 26. 爲硒測試設置系統日期
- 27. 更新系統時間和日期從互聯網
- 28. 更改系統日期和時間的Android編程
- 29. Apache與操作系統的時間差異日期和時間?
- 30. 從字符串在Symfony2中設置日期時間和時間
可能重複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