星期一, 3月 01, 2010

C# 設定系統時間



[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 wMiliseconds;
}
public class SetSystemDateTime
{
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref SystemTime sysTime);

public static bool SetLocalTimeByStr(string timestr)
{
bool flag = false;
SystemTime sysTime = new SystemTime();
DateTime dt = Convert.ToDateTime(timestr);
sysTime.wYear = Convert.ToUInt16(dt.Year);
sysTime.wMonth = Convert.ToUInt16(dt.Month);
sysTime.wDay = Convert.ToUInt16(dt.Day);
sysTime.wHour = Convert.ToUInt16(dt.Hour);
sysTime.wMinute = Convert.ToUInt16(dt.Minute);
sysTime.wSecond = Convert.ToUInt16(dt.Second);
try
{
flag = SetSystemDateTime.SetLocalTime(ref sysTime);
}
catch (Exception e)
{
Console.WriteLine("SetSystemDateTime函数执行异常" + e.Message);
}
return flag;
}
}

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails