C#同步本地机时间与服务器时间

来源:互联网 发布:吉首大学网络管理中心 编辑:程序博客网 时间:2024/04/29 17:30

     #region 修改本地系统时间
        [DllImport("Kernel32.dll")]
        private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

        [DllImport("Kernel32.dll")]
        private extern static uint 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;
        }

        /// <summary>
        /// 将本地时间服务器时间同步
        /// </summary>
        /// <param name="SqlServerTime">时间</param>
        public static void SynchroTime()
        {
            //服务器时间

            DateTime ServerTime = DateTime.Now;
            SYSTEMTIME st = new SYSTEMTIME();
            st.wYear = Convert.ToUInt16(ServerTime.Year);
            st.wMonth = Convert.ToUInt16(ServerTime.Month);
            st.wDay = Convert.ToUInt16(ServerTime.Day);
            st.wHour = Convert.ToUInt16(ServerTime.Hour);
            st.wMilliseconds = Convert.ToUInt16(ServerTime.Millisecond);
            st.wMinute = Convert.ToUInt16(ServerTime.Minute);
            st.wSecond = Convert.ToUInt16(ServerTime.Second);
            SetLocalTime(ref st);
        }
        #endregion

原创粉丝点击