C#修改系统时间

http://s.click.taobao.com/t_1?i=qzfZteiPVqr1Gw%3D%3D&p=mm_16303475_0_0&n=11





[StructLayout(LayoutKind.Sequential)]
    class SystemTime
    {
        ///
        ///系统时间类
        ///        
        public short Year;
        public short Month;
        public short DayOfWeek;
        public short Day;
        public short Hour;
        public short Minute;
        public short Second;
        public short Milliseconds;
    } 
class WinApi
    {
        [DllImport("Kernel32.dll ")]
        public static extern void SetSystemTime([In, Out]   SystemTime st);
    }
//设置时间
        private void setTime(DateTime dt)
        {
            SystemTime st = new SystemTime();
            st.Year = Convert.ToInt16(dt.Year);
            st.Month = Convert.ToInt16(dt.Month);
            st.DayOfWeek = Convert.ToInt16(dt.DayOfWeek);
            st.Day = Convert.ToInt16(dt.Day);
            st.Hour = Convert.ToInt16(dt.Hour);
            st.Minute = Convert.ToInt16(dt.Minute);
            st.Second = Convert.ToInt16(dt.Second);
            st.Milliseconds = Convert.ToInt16(dt.Millisecond);
            if (st.Hour >= 12)
            {
                st.Hour -= (short)8;
            }
            else if (st.Hour >=
            {
                st.Hour -= (short)8;
            }
            else
            {
                st.Hour += (short)16;
            }
            WinApi.SetSystemTime(st);           
        }

你可能感兴趣的:(C++,c,C#)