iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >详解如何利用C#实现设置系统时间
  • 373
分享到

详解如何利用C#实现设置系统时间

C#设置系统时间C#设置时间C#系统时间 2022-12-20 12:12:29 373人浏览 安东尼
摘要

目录实践过程效果代码实践过程 效果 代码 public partial class FORM1 : Form { public Form1() {

实践过程

效果

代码

public partial class FORM1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    //api函数声明   
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
    public extern static bool SetSystemTime(ref SYSTEMTIME time); 
    [StructLayout(LayoutKind.Sequential)]
    public 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 Miliseconds;
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        lblNowTime.Text = DateTime.Now.ToString();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        lblNowTime.Text = DateTime.Now.ToString();
        Microsoft.Win32.SystemEvents.TimeChanged+=new EventHandler(SystemEvents_TimeChanged);
    }

    private void SystemEvents_TimeChanged(object sender, EventArgs e)
    {
        MessageBox.Show("系统日期修改成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mYear = monthCalendar1.SelectionRange.Start.Year;
        mMonth = monthCalendar1.SelectionRange.Start.Month;
        mDay = monthCalendar1.SelectionRange.Start.Day;
        //调用代码   
        SYSTEMTIME t = new SYSTEMTIME();
        t.Year = (short)mYear;
        t.Month = (short)mMonth;
        t.Day = (short)mDay;
        t.Hour = (short)(dateTimePicker1.Value.Hour - 8);//这个函数使用的是0时区的时间,例如,要设12点,则为12-8   
        t.Minute = (short)dateTimePicker1.Value.Minute;
        t.Second = (short)dateTimePicker1.Value.Second;
        bool v = SetSystemTime(ref t);   
    }
    int mYear;
    int mDay;
    int mMonth;
    private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        mYear = e.Start.Year;
        mMonth = e.Start.Month;
        mDay =e.Start.Day;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tabpage1 = new System.Windows.Forms.TabPage();
        this.button2 = new System.Windows.Forms.Button();
        this.button1 = new System.Windows.Forms.Button();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
        this.label2 = new System.Windows.Forms.Label();
        this.lblNowTime = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.tabControl1.SuspendLayout();
        this.tabPage1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // monthCalendar1
        // 
        this.monthCalendar1.Location = new System.Drawing.Point(4, 15);
        this.monthCalendar1.Name = "monthCalendar1";
        this.monthCalendar1.ShowToday = false;
        this.monthCalendar1.ShowTodayCircle = false;
        this.monthCalendar1.TabIndex = 0;
        this.monthCalendar1.TitleBackColor = System.Drawing.Color.LightSkyBlue;
        this.monthCalendar1.TitleForeColor = System.Drawing.Color.Black;
        this.monthCalendar1.TrailingForeColor = System.Drawing.Color.White;
        this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);
        // 
        // tabControl1
        // 
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Location = new System.Drawing.Point(4, 4);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(300, 335);
        this.tabControl1.TabIndex = 1;
        // 
        // tabPage1
        // 
        this.tabPage1.Controls.Add(this.button2);
        this.tabPage1.Controls.Add(this.button1);
        this.tabPage1.Controls.Add(this.groupBox2);
        this.tabPage1.Controls.Add(this.groupBox1);
        this.tabPage1.Location = new System.Drawing.Point(4, 21);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(292, 310);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "时间和日期";
        this.tabPage1.UseVisualStyleBackColor = true;
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(163, 279);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 4;
        this.button2.Text = "取消";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(56, 279);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 3;
        this.button1.Text = "确定";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.dateTimePicker1);
        this.groupBox2.Controls.Add(this.label2);
        this.groupBox2.Controls.Add(this.lblNowTime);
        this.groupBox2.Controls.Add(this.label1);
        this.groupBox2.Location = new System.Drawing.Point(9, 182);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(277, 91);
        this.groupBox2.TabIndex = 2;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "设置时间";
        // 
        // dateTimePicker1
        // 
        this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Time;
        this.dateTimePicker1.Location = new System.Drawing.Point(80, 55);
        this.dateTimePicker1.Name = "dateTimePicker1";
        this.dateTimePicker1.ShowUpDown = true;
        this.dateTimePicker1.Size = new System.Drawing.Size(90, 21);
        this.dateTimePicker1.TabIndex = 3;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(9, 59);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 2;
        this.label2.Text = "设置时间:";
        // 
        // lblNowTime
        // 
        this.lblNowTime.AutoSize = true;
        this.lblNowTime.Location = new System.Drawing.Point(81, 29);
        this.lblNowTime.Name = "lblNowTime";
        this.lblNowTime.Size = new System.Drawing.Size(0, 12);
        this.lblNowTime.TabIndex = 1;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(9, 29);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "当前时间:";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.monthCalendar1);
        this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.groupBox1.Location = new System.Drawing.Point(8, 5);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(278, 169);
        this.groupBox1.TabIndex = 1;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "选择日期";
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(306, 341);
        this.Controls.Add(this.tabControl1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "设置任务栏时间";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.tabControl1.ResumeLayout(false);
        this.tabPage1.ResumeLayout(false);
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.MonthCalendar monthCalendar1;
    private System.Windows.Forms.TabControl tabControl1;
    private System.Windows.Forms.TabPage tabPage1;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.DateTimePicker dateTimePicker1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label lblNowTime;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Timer timer1;

}

到此这篇关于详解如何利用C#实现设置系统时间的文章就介绍到这了,更多相关C#设置系统时间内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解如何利用C#实现设置系统时间

本文链接: https://www.lsjlt.com/news/175295.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解如何利用C#实现设置系统时间
    目录实践过程效果代码实践过程 效果 代码 public partial class Form1 : Form { public Form1() { ...
    99+
    2022-12-20
    C#设置系统时间 C#设置时间 C# 系统时间
  • win10系统时间与Internet时间如何同步设置
    这篇文章主要介绍win10系统时间与Internet时间如何同步设置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先登录到win10系统桌面,然后点击任务栏右侧的时间显示区域,点击打开“更改日期和时间设置...”。...
    99+
    2023-06-28
  • centos7系统如何设置锁屏时间
    这篇文章主要介绍了centos7系统如何设置锁屏时间,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。centos7 系统设置锁屏时间的好处就是可以保护隐私,让自己的电脑更安全,...
    99+
    2023-06-10
  • Python如何使用http时间同步设置系统时间源码
    这篇文章给大家介绍Python如何使用http时间同步设置系统时间源码,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Python方式实现使用http时间同步设置系统时间源码,系统环境是ubuntu 12.04、Pyth...
    99+
    2023-06-02
  • 如何设置win10系统免打扰时间
    这篇文章主要介绍了如何设置win10系统免打扰时间,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体方法按下键盘上的“win”徽标键和“R”按键,在出现的运行窗口中输入命令“...
    99+
    2023-06-27
  • C语言系统日期和时间实例详解
    目录⒈题目内容⒉题目要求⒊思考问题⒋解题思路¹time - 库函数²localtime - 库函数⒌程序代码 ⒍代码运行结果总结⒈题目内容 输出系统的日...
    99+
    2024-04-02
  • Linux中如何查看系统时间和设置时区
    要查看系统时间,可以在命令行中输入以下命令: date 要设置系统时区,可以在命令行中输入以下命令: sudo timedatec...
    99+
    2024-03-13
    Linux
  • C++设计与实现ORM系统实例详解
    目录介绍依赖关系设计思路项目进度数据库通用接口实例构造智能查询方式设计单元测试运行方法介绍 我们通用的ORM,基本模式都是想要脱离数据库的,几乎都在编程语言层面建立模型,由程序去与数...
    99+
    2024-04-02
  • 联想专用win7系统如何设置休眠时间
    即使停止供电,休眠操作会将当前内存运行的数据保存在硬盘中,以避免丢失。让我们一起来了解一下如何设置联想电脑win7系统的休眠时间,这样可以快速启动电脑。1、首先要点击win7桌面左下角的开始菜单,选择控制面板。2、在控制面板里点击系统和安全...
    99+
    2023-07-12
  • Python调用系统命令设置超时时间
        python通过subprocess模块调用系统命令。实际使用中,有一次是命令进入了交互模式,结果web端直接卡死了。调用时设置一个超时时间,时间用完后自动断开。这样就避免了系统因为调用命令而僵死的问题。def sys_comman...
    99+
    2023-01-31
    命令 时间 系统
  • Pythontime时间格式化和设置时区实现代码详解
    目录1、时间戳转换为指定格式日期2、将字符串的时间转换为时间戳3、Datetime详细介绍4、获得三天前的时间的方法5、使用datetime模块来获取当前的日期和时间1、时间戳转换为...
    99+
    2023-02-24
    Python time时间格式化 Python time设置时区
  • 利用Java如何实现获取UTC时间
    本篇文章为大家展示了利用Java如何实现获取UTC时间,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。本文实例讲述了Java获取UTC时间的方法。分享给大家供大家参考,具体如下:取得本地时间:java...
    99+
    2023-05-31
    java utc时间 ava
  • c语言如何获取系统时间
    要在C语言中获取系统时间,可以使用 <time.h> 头文件中的函数。以下是一些获取系统时间的常用函数: time()...
    99+
    2024-04-02
  • golang如何设置系统时区
    golang设置系统时区的方法,在windows系统下:1、点击任务栏上的“时间和日期”;2、在显示的面板中,点击“更改日期和时间设置”;3、在“日期和时间”选项下,点击“更改时区”;4、再选择所需的时区,并点击“确定”。在linux系统下...
    99+
    2023-07-12
  • 如何设置Linux系统的空闲等待时间TMOUT
    这篇文章主要为大家展示了“如何设置Linux系统的空闲等待时间TMOUT”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何设置Linux系统的空闲等待时间TMOUT”这篇文章吧。为了增强Linu...
    99+
    2023-06-09
  • 如何在FreeBSD系统上设置时间同步服务
    在FreeBSD系统上设置时间同步服务可以通过使用NTP(Network Time Protocol)来实现。下面是在FreeBSD...
    99+
    2024-04-02
  • Linux系统如何设置时区
    这篇文章主要为大家展示了Linux系统如何设置时区,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带大家一起来研究并学习一下“Linux系统如何设置时区”这篇文章吧。查看Linux当前时区你可以使用如下命令非常容易地就查看到 ...
    99+
    2023-06-28
  • C++实现统计代码运行时间的示例详解
    目录纯标准库实现类似C#的实现总结本来想自己写的,一看github上面都有就不再重复造轮子了。github上的项目如下: StopWatch 纯标准库实现:使用std::chrono...
    99+
    2023-05-19
    C++统计代码运行时间 C++ 代码运行时间 C++统计时间
  • C/C++实现获取系统时间的示例代码
    目录概述示例易用性封装概述 C 标准库提供了 time() 函数与 localtime() 函数可以获取到当前系统的日历时间,但 time() 函数精度只能到秒级,如果需要更高精度的...
    99+
    2022-12-20
    C++获取系统时间 C++ 系统时间 C++获取时间
  • Python time时间格式化和设置时区实现代码详解
    目录1、时间戳转换为指定格式日期2、将字符串的时间转换为时间戳3、Datetime详细介绍4、获得三天前的时间的方法5、使用datetime模块来获取当前的日期和时间1、时间戳转换为...
    99+
    2023-02-25
    Python time时间格式化 Python time设置时区
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作