C# 设置系统时间

本文介绍如何在C#程序中修改系统时间,提供了一段实现这一功能的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在程序中有时需要系统时间准确,为了方便修改时间,做了个时间修改的,代码如下

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  Qualification
{
    
public partial class FormDateSet : Form
    
{
        
public FormDateSet()
        
{
            InitializeComponent();

        }

        
//调用Kernel32.DLL
        [DllImport("Kernel32.dll")]
        
public static extern void GetLocalTime(SystemTime st);
        [DllImport(
"Kernel32.dll")]
        
public static extern void SetLocalTime(SystemTime st);


        [StructLayout(LayoutKind.Sequential)]
        
public class 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;
        
        }

        
private void FormDateSet_Load(object sender, EventArgs e)
        
{
           
        }

        
//取得当前系统时间
        private void timer1_Tick(object sender, EventArgs e)
        
{
            SystemTime st 
= new SystemTime();
            GetLocalTime(st);
            
this.textBox1.Text =  st.wYear.ToString()+"-";
            
this.textBox1.Text = this.textBox1.Text + st.wMonth.ToString() + "-";
            
this.textBox1.Text = this.textBox1.Text + st.wDay.ToString() + " ";
            
this.textBox1.Text = this.textBox1.Text + st.Whour.ToString() + ":" + st.wMinute.ToString() + ":" + st.wSecond.ToString();
        }


        
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        
{
            
        }

//校对系统时间
        private void Btn_set_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
                SystemTime st 
= new SystemTime();
                st.wYear 
= (ushort)this.dateTimePicker1.Value.Year;
                st.wMonth 
= (ushort)this.dateTimePicker1.Value.Month;
                st.wDay 
= (ushort)this.dateTimePicker1.Value.Day;
                st.Whour 
= (ushort)this.dateTimePicker1.Value.Hour;
                st.wMinute 
= (ushort)this.dateTimePicker1.Value.Minute;
                st.wSecond 
= (ushort)this.dateTimePicker1.Value.Second;
                SetLocalTime(st);
                MessageBox.Show(
"系统时间设置成功!""系统时间设置成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
catch
            
{
                MessageBox.Show(
"系统时间设置失败!""设置失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
finally
            
{}
        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值