Setting Date & Time w/C#

  • Thread starter Thread starter Dave Lech
  • Start date Start date
D

Dave Lech

I need to be able to set the date and time on a PocketPC
device programmatically using C# in a CompactFramework
WinForms application. Basically I need to synchronize
the time with the database server. I haven't been able to
find any documentation on how to do this anywhere. Any
help would be greatly appreciated. Thanx in advance.
 
Here is some code I happen to have lying around - not production quality but
a good sample:

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace SetSystemTime
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label lblOriginalLocal;
private System.Windows.Forms.Label lblOriginalUtc;
private System.Windows.Forms.Label lblNewLocal;
private System.Windows.Forms.Label lblNewUtc;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.lblOriginalLocal = new System.Windows.Forms.Label();
this.lblOriginalUtc = new System.Windows.Forms.Label();
this.lblNewLocal = new System.Windows.Forms.Label();
this.lblNewUtc = new System.Windows.Forms.Label();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 240);
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Silver;
this.panel1.Controls.Add(this.lblOriginalUtc);
this.panel1.Controls.Add(this.lblOriginalLocal);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 8);
this.panel1.Size = new System.Drawing.Size(240, 100);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Size = new System.Drawing.Size(136, 20);
this.label1.Text = "Local";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.label2.Location = new System.Drawing.Point(8, 64);
this.label2.Size = new System.Drawing.Size(136, 20);
this.label2.Text = "UTC";
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 8);
this.label3.Text = "Original Time";
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.Silver;
this.panel2.Controls.Add(this.lblNewUtc);
this.panel2.Controls.Add(this.lblNewLocal);
this.panel2.Controls.Add(this.label4);
this.panel2.Controls.Add(this.label5);
this.panel2.Controls.Add(this.label6);
this.panel2.Location = new System.Drawing.Point(0, 120);
this.panel2.Size = new System.Drawing.Size(240, 100);
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 8);
this.label4.Text = "New Time";
//
// label5
//
this.label5.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.label5.Location = new System.Drawing.Point(8, 64);
this.label5.Size = new System.Drawing.Size(136, 20);
this.label5.Text = "UTC";
//
// label6
//
this.label6.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.label6.Location = new System.Drawing.Point(8, 32);
this.label6.Size = new System.Drawing.Size(136, 20);
this.label6.Text = "Local";
//
// lblOriginalLocal
//
this.lblOriginalLocal.Location = new System.Drawing.Point(152,
32);
this.lblOriginalLocal.Size = new System.Drawing.Size(80, 20);
//
// lblOriginalUtc
//
this.lblOriginalUtc.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.lblOriginalUtc.Location = new System.Drawing.Point(152,
64);
this.lblOriginalUtc.Size = new System.Drawing.Size(80, 20);
//
// lblNewLocal
//
this.lblNewLocal.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.lblNewLocal.Location = new System.Drawing.Point(152, 32);
this.lblNewLocal.Size = new System.Drawing.Size(80, 20);
//
// lblNewUtc
//
this.lblNewUtc.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.lblNewUtc.Location = new System.Drawing.Point(152, 64);
this.lblNewUtc.Size = new System.Drawing.Size(80, 20);
//
// Form1
//
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}
public struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Millisecond;
};

[DllImport("kernel32.dll", EntryPoint="GetSystemTime",
SetLastError=true)]
public extern static void Win32GetSystemTime(ref SystemTime st);

[DllImport("coredll.dll", EntryPoint="GetSystemTime",
SetLastError=true)]
public extern static void WinCEGetSystemTime(ref SystemTime st);

[DllImport("kernel32.dll", EntryPoint="SetSystemTime",
SetLastError=true)]
public extern static bool Win32SetSystemTime(ref SystemTime st);

[DllImport("coredll.dll", EntryPoint="SetSystemTime",
SetLastError=true)]
public extern static bool WinCESetSystemTime(ref SystemTime st);

private void button1_Click(object sender, System.EventArgs e)
{

try
{
SystemTime st = new SystemTime();

//WinCEGetSystemTime(ref st);

//st.Hour = 1;
//st.Minute = 30;

DateTime dt = DateTime.UtcNow;
this.lblOriginalUtc.Text = dt.ToShortTimeString();
st.Year = (ushort)dt.Year;
st.Month = (ushort)dt.Month;
st.Day = (ushort)dt.Day;
st.DayOfWeek = (ushort)dt.DayOfWeek;
st.Hour = (ushort)((dt.Hour + 1) % 24);
st.Minute = (ushort)dt.Minute;
st.Second = 59;
st.Millisecond = (ushort)dt.Millisecond;
this.lblOriginalLocal.Text =
dt.ToLocalTime().ToShortTimeString();

bool result = WinCESetSystemTime(ref st);
if (true != result)
{
int error = Marshal.GetLastWin32Error();
Debug.WriteLine(String.Format("SetSystemTime failed:
{0:X8}", error));
}
dt = DateTime.UtcNow;
this.lblNewUtc.Text = dt.ToShortTimeString();
this.lblNewLocal.Text =
dt.ToLocalTime().ToShortTimeString();
}
catch (Exception x)
{
Debug.WriteLine(x);
}
}
}
}

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
You've seen the examples on setting the device time, but
you still need to get the time from a server.

The way we have done this is by using a web service that
provides several methods used by our app one of them being
to return the time on the server.

Chris
 
Hi all, i have more or less the same problem:

i need to set the device date/time programmatically but from a desktop application.
this application use OpenNETCF Descktop Comunication library and my device has no connection so i need to get the right time from the desktop.

Any suggestion??

Thanks
 
Back
Top