ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#修改系统时间

C#修改系统时间

原创 Linux操作系统 作者:fangqm 时间:2010-06-02 23:59:43 0 删除 编辑
 
  #region 调用API更改时间
        [DllImport("kernel32.dll")]
        private static extern bool SetLocalTime(ref SYSTEMTIME time);
        [StructLayout(LayoutKind.Sequential)]
        private struct 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;
        }
        public static void SetDate(DateTime dt)
        {
            SYSTEMTIME st;
            st.year = (short)dt.Year;
            st.month = (short)dt.Month;
            st.dayOfWeek = (short)dt.DayOfWeek;
            st.day = (short)dt.Day;
            st.hour = (short)dt.Hour;
            st.minute = (short)dt.Minute;
            st.second = (short)dt.Second;
            st.milliseconds = (short)dt.Millisecond;
            SetLocalTime(ref st);
        }
        #endregion
 
使用时,直接调用SetDate(DateTime dt)
即可

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-664282/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
创业项目 https://u.rscode.cn

注册时间:2010-03-24

  • 博文量
    297
  • 访问量
    784628