• C#实现设置系统时间


    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace Demo
    {
        public partial class 设置系统时间 : Form
        {
            public 设置系统时间()
            {
                InitializeComponent();
            }
    
            private void btnSetting_Click(object sender, EventArgs e)
            {
                //取得当前系统时间
                DateTime t = DateTime.Now;
                //在当前时间上加上一周
                t = t.AddDays(7);
    
                t = DateTime.Parse(this.dtpTime.Text);
    
                //转换System.DateTime到SYSTEMTIME
                SYSTEMTIME st = new SYSTEMTIME();
                st.FromDateTime(t);
    
    
                //调用Win32 API设置系统时间
                Win32API.SetLocalTime(ref st);
                //显示当前时间
               // MessageBox.Show(DateTime.Now.ToString());
    
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        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 wMilliseconds;
    
            /// <summary>
            /// 从System.DateTime转换。
            /// </summary>
            /// <param name="time">System.DateTime类型的时间。</param>
            public void FromDateTime(DateTime time)
            {
                wYear = (ushort)time.Year;
                wMonth = (ushort)time.Month;
                wDayOfWeek = (ushort)time.DayOfWeek;
                wDay = (ushort)time.Day;
                wHour = (ushort)time.Hour;
                wMinute = (ushort)time.Minute;
                wSecond = (ushort)time.Second;
                wMilliseconds = (ushort)time.Millisecond;
            }
            /// <summary>
            /// 转换为System.DateTime类型。
            /// </summary>
            /// <returns></returns>
            public DateTime ToDateTime()
            {
                return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
            }
            /// <summary>
            /// 静态方法。转换为System.DateTime类型。
            /// </summary>
            /// <param name="time">SYSTEMTIME类型的时间。</param>
            /// <returns></returns>
            public static DateTime ToDateTime(SYSTEMTIME time)
            {
                return time.ToDateTime();
            }
        }
        public class Win32API
        {
            [DllImport("Kernel32.dll")]
            public static extern bool SetLocalTime(ref SYSTEMTIME Time);
            [DllImport("Kernel32.dll")]
            public static extern void GetLocalTime(ref SYSTEMTIME Time);
        }
    
    }
    

      

  • 相关阅读:
    线性代数:矩阵行列式
    线性代数:逆变换
    线性代数:线性变换
    线性代数:零空间
    线性代数:向量乘法
    线性代数基础:向量组合
    线性代基础理论:向量
    线性代基础理论:矩阵
    SpringBoot 消费NSQ消息
    将Oracle中的数据放入elasticsearch
  • 原文地址:https://www.cnblogs.com/YYkun/p/9171447.html
Copyright © 2020-2023  润新知