|
如何设置PDA设备的时间 (C#)
http://bbs.pdafans.com/thread-411292-1-1.html
最近开发时遇到PDA访问服务器时要和设置服务器的时间到PDA(C#中要修改系统时间真不如C++方便 ),参考了http://blog.eeworld.net/iorikyo/archive/2006/10/01/1314892.aspx的文章后通过下面这个类解决了修改PDA时间的问题。代码如下:
using System;
using System.Runtime.InteropServices;
namespace setlocaltime
{
class Systemtime
{
[DllImport("coredll.dll"]
public static extern bool SetLocalTime(ref SYSTEMTIME Time);
[DllImport("coredll.dll"]
public static extern void GetLocalTime(ref SYSTEMTIME Time);
}
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;
///
/// 从System.DateTime转换。
///
/// System.DateTime类型的时间。
public void FromDateTime(DateTime time)
{
wYear = (ushort)time.Year;
wMonth = (ushort)time.Month;
wDayOfWeek = (ushort)time.DayOfWeek;
wDay = (ushort)time.Day;
wHour = (ushort)time.Hour;
wMinute = (ushort)time.Minute;
wSecond = (ushort)time.Second;
wMilliseconds = (ushort)time.Millisecond;
}
///
/// 转换为System.DateTime类型。
///
///
public DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
}
///
/// 静态方法。转换为System.DateTime类型。
///
/// SYSTEMTIME类型的时间。
///
public static DateTime ToDateTime(SYSTEMTIME time)
{
return time.ToDateTime();
}
}
} |
|