2010-05-04 50 views
1

我有一個使用.Net Compact框架工作3.5開發的應用程序,我需要在每個Web服務調用中將設備日期和時間與中央服務器同步。從應用程序設置系統日期和時間

謝謝。

+2

可能重複http://stackoverflow.com/questions/738615/set-device-time-on-net-cf – 2010-05-04 06:14:46

+2

@Sundar:很多沒有一度接受答案的問題。開始標記答案或不要驚訝,人們不回答你。 – Shaihi 2010-05-04 06:33:55

回答

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

相關問題