用C#修改WM5.0和WM6.0 系统平台的时间
2010-11-25 22:40
295 查看
用C#在windowsMobile5.0/6.0平台上进行修改系统时间。
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using DigitalCity.Terminal.Loglib; namespace ModifyTime { /// <summary> /// 实现修改本地时间的功能 /// </summary> public class SyncSystemTime { /// <summary> /// 根据传入的时间信息,修改本地时间 /// </summary> /// <param name="serviceTime">传入的时间信息</param> public void SetLocalSystemTime(string serviceTime) { try { // 系统时间结构 SYSTEMTIME systime = new SYSTEMTIME(); // 截取年信息 systime.wYear = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(0 , 4)) ); // 截取月信息 systime.wMonth = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(4 , 2)) ); // 截取日信息 systime.wDay = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(6 , 2)) ); // 截取小时信息 systime.wHour = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(8 , 2)) ); if ( systime.wHour < 8 ) { systime.wDay = ( ushort ) ( systime.wDay - 1 ); if ( systime.wDay <= 0 ) { systime.wMonth = ( ushort ) ( systime.wMonth - 1 ); if ( systime.wMonth <= 0 ) { systime.wMonth = 12; systime.wYear = ( ushort ) ( systime.wYear - 1 ); } switch ( systime.wMonth ) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: systime.wDay = 31; break; case 2: if ( ( systime.wYear / 4 == 0 ) || ( ( systime.wYear / 100 == 0 ) && ( systime.wYear / 400 == 0 ) ) ) { systime.wDay = 29; } else { systime.wDay = 28; } break; default: systime.wDay = 30; break; } } } // 将时间转为东八区时间 systime.wHour = ( ushort ) ( ( systime.wHour + 24 - 8 ) % 24 ); // 截取分钟信息 systime.wMinute = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(10 , 2)) ); // 截取秒信息 systime.wSecond = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(12 , 2)) ); // 调用系统方法,设置系统实际 SetSystemTime(ref systime); } catch ( Exception ) { Log.log.WriteIntoFile("传入的服务器时间不正确,引发异常"); } } [DllImport("coredll.dll")] private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime); /// <summary> /// 系统时间结构 /// </summary> private 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; } } // end of class } // end of namespace
相关文章推荐
- C#修改本地系统时间转载
- c#调用win32 API函数修改系统时间
- C#修改系统时间
- C#修改系统时间
- C# 修改系统本地时间
- c# 修改系统本地时间
- 互芯平台系统时间修改
- c#修改系统时间的方法
- C#.net 修改系统时间
- c# 修改系统本地时间
- C# 修改系统时间
- C#修改系统时间(本机电脑)
- C#利用win32 Api 修改本地系统时间、获取硬盘序列号
- CentOS6 x64位平台下,JDK1.6运行环境下,Thread.sleep()的睡眠时间会因为修改系统时钟而改变。
- c#修改系统时间的方法
- c# 编程修改 wince 系统时间
- 用C#语言修改系统时间
- c#修改系统时间的方法
- c#修改系统时间的方法
- C#实现修改系统时间的方法