您的位置:首页 > 编程语言 > C#

C# 修改系统本地时间

2011-09-25 13:37 351 查看
using System;

using System.Runtime.InteropServices;

using System.Windows.Forms;
namespace SystemTime

{

    public partial class FormLocal : Form

    {

        #region SetLocalTime

        [DllImport("Kernel32.dll")]

        private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);

        [StructLayout(LayoutKind.Sequential)]

        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;

        }

        private void SetSystemTime(DateTime date)

        {

            SYSTEMTIME lpTime = new SYSTEMTIME();

            lpTime.wYear = Convert.ToUInt16(date.Year);

            lpTime.wMonth = Convert.ToUInt16(date.Month);

            lpTime.wDayOfWeek = Convert.ToUInt16(date.DayOfWeek);

            lpTime.wDay = Convert.ToUInt16(date.Day);

            DateTime time = DateTime.Now;

            lpTime.wHour = Convert.ToUInt16(time.Hour);

            lpTime.wMinute = Convert.ToUInt16(time.Minute);

            lpTime.wSecond = Convert.ToUInt16(time.Second);

            lpTime.wMilliseconds = Convert.ToUInt16(time.Millisecond);

            SetLocalTime(ref lpTime);

        }

        #endregion

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# struct date class