一:截图

二:代码

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);//执行按钮点击事件
}
}
}
}

调用系统api修改系统时间的更多相关文章

  1. 调用windows api 获取系统分辨率

    c++中: int cxScreen,cyScreen; cxScreen=GetSystemMetrics(SM_CXSCREEN); cyScreen=GetSystemMetrics(SM_CY ...

  2. C#技术点--修改系统时间

    C#的System.DateTime类提供了对日期时间的封装,用它进行时间的转换和处理很方便,但是我没有在其中找到任何可以用来修改系统时间的成员.用过VC.VB等的朋友可能知道,我们可以调用Win32 ...

  3. Wince修改系统时间问题

           当我们需要修改到系统时间的时候,需要用到下面四个函数:SetLoaclTime,GetLocalTime,SetSystemTime,GetSystemTime.这四个函数是用来修改或者 ...

  4. Linux修改系统时间与时区

    GMT  (Greewich Mean Time) 格林威治标准时间:GMT是老的时间计量标准,根据地球的自转和公转来计算时间,也就是太阳每天经过位于英国伦敦郊区的皇家格林尼治天文台的标准时间就是中午 ...

  5. C/C++ 修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析

    修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析 介绍 最近修复项目问题时,发现当系统时间往前修改后,会导致sem_timedwait函数一直阻塞.通过搜索了发现int sem_ ...

  6. Android 使用date set命令修改系统时间

    测试环境:android 7.1.1 在adb shell中试图使用 date -s "yyyymmdd.[[[hh]mm]ss]"修改系统系统时间时,会提示 date: Unkn ...

  7. Centos7.4永久修改系统时间

    [root@V3B01-zsy yum.repos.d]# date -s "2019-09-24 17:02:30" 2019年 09月 24日 星期二 17:02:30 CST ...

  8. 在 Xen 虚拟机下修改系统当前时间

    在 Xen 虚拟机下修改系统当前时间 Xen 虚拟机默认不允许不同的虚拟机使用不同的系统时间,因此所有虚拟机的系统时间都会同宿主机的系统时间严格同步,用 date 命令修改虚拟机系统时间时虽然提示成功 ...

  9. Linux永久修改系统时间和时区方法

    修改时区: 1> 找到相应的时区文件 /usr/share/zoneinfo/Asia/Shanghai 用这个文件替换当前的/etc/localtime文件. 或者找你认为是标准时间的服务器, ...

随机推荐

  1. WebForm开发常用代码

    1.获取服务器绝对路径: public static string GetMapPath(string strPath) { if (HttpContext.Current != null) { re ...

  2. java simple check whether a file or directory.

    Ref:  check whether a file or directory First, make sure the path exists by using: new File(path).ex ...

  3. RAC配置、安装

    RAC  配置及安装 2012年12月30日 星期日 21:49 ******************************************************************* ...

  4. 浅谈html5某些新元素的用途

    大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...

  5. Problem 1183 - 排列

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int c ...

  6. POJ 2186.Popular Cows (强连通)

    强连通缩点,统计入度为1的缩点后的点的个数 个数1的话输出这个强连通分量的点的数量 否则输出0: code /* Kosaraju算法,无向图的强连通分量,时间复杂度O(n+m) 思路: 按照图G的深 ...

  7. 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  8. 使用程序往Neo4j导入CSV报错

    今天在用程序向Neo4j导入csv文件时,报以下错误: java.net.ConnectException: Connection refused: connect java.rmi.ConnectE ...

  9. Chrome rem bug

    遇到一个bug,发现chrome在初始化页面的时候,会错误的渲染rem单位,导致字体过大. 比如: 正常的应该是这样的: 原因是,为了使用rem单位,我们常常将 html 的font-size设置为6 ...

  10. php 上传视频的代码

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...