从计算机设置Windows Mobile设备的系统时间? [英] Setting Windows Mobile device system time from computer?

查看:172
本文介绍了从计算机设置Windows Mobile设备的系统时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同步的设备,以它的停靠,我通过紧凑的框架(C#)的工作桌面的系统日期和时间。

I'm trying to sync the system date and time of a device to the desktop it's docked to, I'm working through compact framework (C#).

我的目标是检测时,该设备被同步到台式机上的数据库是最后一次了。

My goal is to detect when the last time the device was synced to a database on the desktop machine was.

我怎样才能做到这一点?

How can I do this?

推荐答案

下面是我一直使用的。我把我的设备同步上涨首次数据库对象开始。

Here's the one I have been using. I make my devices sync up the first time the database object is initiated.

#if PocketPC
[DllImport("coredll.dll")]
#else
[DllImport("kernel32.dll")]
#endif
private static extern bool SetLocalTime([In] ref SYSTEMTIME lpLocalTime);



...是的,我使用相同的代码为我的Windows电脑作为我WinMobile电脑。

...and yes, I use the same code for my Windows PCs as for my WinMobile PCs.

野点变量是全局的,我的整个项目,并定义为1月1日1900年希望!没有在这家公司发生在SQL Server中的日期

This NODATE variable is global to my entire project, and is defined as Jan. 1, 1900. Hopefully, nothing at this company happened on that date in the SQL Server!

开始一个结构:

/// <summary>
/// SYSTEMTIME structure with some useful methods
/// </summary>
public struct SYSTEMTIME {
  public short Year, Month, DayOfWeek, Day, Hour, Minute, Second, Millisecond;
  /// <summary>
  /// Convert form System.DateTime
  /// </summary>
  /// <param name="time">Creates System Time from this variable</param>
  public void FromDateTime(DateTime time) {
    Year = (short)time.Year;
    Month = (short)time.Month;
    DayOfWeek = (short)time.DayOfWeek;
    Day = (short)time.Day;
    Hour = (short)time.Hour;
    Minute = (short)time.Minute;
    Second = (short)time.Second;
    Millisecond = (short)time.Millisecond;
  }
  /// <summary>
  /// Convert to System.DateTime
  /// </summary>
  public DateTime ToDateTime() {
    return new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
  }
  /// <summary>
  /// STATIC: Convert to System.DateTime
  /// </summary>
  public static DateTime ToDateTime(SYSTEMTIME time) {
    return time.ToDateTime();
  }
}



然后,只需使用结构像我这种方法做,使通话(我同步与我们的SQL Server)

Then just use that structure like I do in this method that makes the call (I sync with our SQL Server).

/// <summary>
/// Synchronize the Time on the SQL Server with the time on the device
/// </summary>
public static int SyncWithSqlTime() { // pass in SQL Time and set time on device
  SYSTEMTIME st = new SYSTEMTIME();
  DateTime sqlTime = Global.NODATE;
  using (SqlCommand cmd = new SqlCommand("SELECT GETDATE() AS CurrentDateTime;", new SqlConnection(DataSettings.DatabaseConnectionString))) {
    try {
      cmd.Connection.Open();
      SqlDataReader r = cmd.ExecuteReader();
      while (r.Read()) {
        if (!r.IsDBNull(0)) {
          sqlTime = (DateTime)r[0];
        }
      }
    } catch (Exception) {
      return -1;
    }
  }
  if (sqlTime != Global.NODATE) {
    st.FromDateTime(sqlTime); // Convert to SYSTEMTIME
    if (SetLocalTime(ref st)) { //Call Win32 API to set time
      return 1;
    }
  }
  return 0;
}

这是最后一个如果在底部的条件是我使用 SetLocalTime 设置时间。

That last if condition at the bottom is where I set the time using SetLocalTime.

希望这可以让你的地方,结果
〜乔

Hope this gets you somewhere,
~Joe

这篇关于从计算机设置Windows Mobile设备的系统时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆