用c#编程修改wince系统时间:
1.需要调用动态库
coredll.dll
2. 头文件需添加using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
3.在定义结构体时,必须包含8个参数,如少一个则会出现错误
因为在调用SetLocalTime时是通过指针的形式在调用,如果结构体中少了参数, 那么SetLocalTime寻找地址时会出错
下面为实现用c#修改wince系统时间代码:
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME {
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
[DllImport("coredll.dll")]
private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);
[DllImport("coredll.dll")]
private static extern bool GetLocalTime(ref SYSTEMTIME lpSystemTime);
public void SetSysTime(DateTime date)
{
SYSTEMTIME lpTime = new SYSTEMTIME();
lpTime.wYear = Convert.ToUInt16(date.Year);
lpTime.wMonth = Convert.ToUInt16(date.Month);
lpTime.wDay = Convert.ToUInt16(date.Day);
lpTime.wHour = Convert.ToUInt16(date.Hour);
lpTime.wMinute = Convert.ToUInt16(date.Minute);
lpTime.wSecond = Convert.ToUInt16(date.Second);
SetLocalTime(ref lpTime);
}