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);
} }
C#实现设置系统时间的更多相关文章
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
- Qt设置系统时间(使用SetSystemTime API函数)
大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime(); ...
- Linux 设置系统时间和日期 API
嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...
- ubuntu设置系统时间与网络时间同步
ubuntu设置系统时间与网络时间同步 Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Ker ...
- ubuntu设置系统时间与网络时间同步和时区
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
- delphi中设置系统时间方法
procedure TMainFrm.Timer1Timer(Sender: TObject); var systemtime:Tsystemtime; dt:TDateTime; begin ...
- CentOS设置系统时间、硬件时间、以及定时校对时间
CentOS设置系统时间和时区 一.设置时区 方法一:使用setup工具 setup 选择Timezone configuration 选择Asia/Shanghai 空格键勾选上System clo ...
- linux设置系统时间
设置系统时间 - date命令:显示系统的时间,可以在直接输入"date"命令来查看系统的时间 - date+%y/%m/%d - ...
随机推荐
- scala quick check
Scala 特性 面向对象特性 函数式编程 Scala也是一种函数式语言,其函数也能当成值来使用.Scala提供了轻量级的语法用以定义匿名函数,支持高阶函数,允许嵌套多层函数,并支持柯里化.Sca ...
- spacemacs conf
> da100 src $ cat ~/.spacemacs (defun dotspacemacs/layers () (setq-default dotspacemacs-distribut ...
- Maxscale-在第一个节点的配置
[maxscale]threads=4 ##### Write Service, need to set address[server1]type=serveraddress=172.16.50.36 ...
- angular file change
AngularJs: How to check for changes in file input fields? <input type="file" onchange=& ...
- Unity 3D与Android Studio安卓交互之-导出jar包
u3d与安卓 jar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...
- jqGrid 加载完jqGrid之后可以执行函数的方法
, gridComplete: function() { jQuery('#first_gridpager').html("首页 "); jQuery('#prev_gridpag ...
- java-Random类
1.Random类的概述和方法使用 * A:Random类的概述 * 此类用于产生随机数 * 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数 ...
- java-方法重写的注意事项
1.父类中私有方法不能被重写.因为父类的私有方法子类根本就无法继承. 2.子类重写父类方法时,访问权限不能更低.最好就一致. 3.父类静态方法,子类也必须通过静态方法进行重写.其实这个算不上方法重写, ...
- SQL-数据库操作-002
数据库的创建:create database data_name on primary ( name=data_1, filename='D\Data\data_name.mdf', size=50m ...
- [动态差分+二维前缀和][小a的轰炸游戏]
链接:https://ac.nowcoder.com/acm/contest/317/E来源:牛客网 题目描述 小a正在玩一款即时战略游戏,现在他要用航空母舰对敌方阵地进行轰炸 地方阵地可以看做是n× ...