執行個體365(2)---------調用系統api修改系統時間

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

一:

二:代碼
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace SetDate{    public partial class Frm_Main : Form    {        public Frm_Main()        {            InitializeComponent();        }        public class SetSystemDateTime//自訂類SetSystemDateTime,用於設定系統日期,為了使用DllImportAttribute類(DllImportAttribute類是指可以將屬性應用於方法,            //並由非託管動態連結程式庫(DLL)作為靜態進入點公開),            //需要引入命名空間:using System.Runtime.InteropServices;        {            [DllImportAttribute("Kernel32.dll")]//使用包含要匯入的方法的 DLL 的名稱初始化 DllImportAttribute 類的新執行個體。            public static extern void GetLocalTime(SystemTime st);//C#要設定系統時間必須要調用Win32的API,而其中相關的函數就是SetSystemTime(), GetSystemTimer(), SetLocalTime(), GetLocalTime(),            //這似乎是用VC寫的函數,在VC++中是可以直接調用的。            //對於這兩個函數,其輸入參數必須是一個下面這樣的結構體,其成員變數類型必須是ushort,成員變數不能改變順序。            [DllImportAttribute("Kernel32.dll")]            public static extern void SetLocalTime(SystemTime st);        }        [StructLayoutAttribute(LayoutKind.Sequential)]        public class SystemTime//自訂類SystemTime用於定義日期類        {            public ushort vYear;//年            public ushort vMonth;//月            public ushort vDayOfWeek;//星期            public ushort vDay;//日            public ushort vHour;//小時            public ushort vMinute;//分            public ushort vSecond;//秒        }        private void button1_Click(object sender, EventArgs e)        {            this.textBox1.Text = DateTime.Now.ToString("F") +//得到系統時間                "  " + DateTime.Now.ToString("dddd");        }        private void button2_Click(object sender, EventArgs e)        {            if (MessageBox.Show("您真的確定更改系統當前日期嗎?",//設定系統當前日期時間                "資訊提示", MessageBoxButtons.OK) == DialogResult.OK)            {                DateTime Year = this.dateTimePicker1.Value;//得到時間資訊                SystemTime MySystemTime = new SystemTime();//建立系統時間類的對象                SetSystemDateTime.GetLocalTime(MySystemTime);//得到系統時間                MySystemTime.vYear = (ushort)this.dateTimePicker1.Value.Year;//設定年                MySystemTime.vMonth = (ushort)this.dateTimePicker1.Value.Month;//設定月                MySystemTime.vDay = (ushort)this.dateTimePicker1.Value.Day;//設定日                MySystemTime.vHour = (ushort)this.dateTimePicker2.Value.Hour;//設定小時                MySystemTime.vMinute = (ushort)this.dateTimePicker2.Value.Minute;//設定分                MySystemTime.vSecond = (ushort)this.dateTimePicker2.Value.Second;//設定秒                SetSystemDateTime.SetLocalTime(MySystemTime);//設定系統時間                button1_Click(null, null);//執行按鈕點擊事件            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.