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 - ...
随机推荐
- css与html 与js的基础语法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_2-23456.a ...
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- java的数组和arraylist
1.数组 1.0 一开始就错了 int a[8]; //没有像C在内存中开辟了8个区域 改: int a[] = {1,2,3} ; System.out.println(a.length); ...
- 二分法求常数E,常数π,根号2
进行单元测试数据____________________________________ public class Test { public static void main(String[] ar ...
- Connecting Elixir Nodes with libcluster, locally and on Kubernetes
转自:https://www.poeticoding.com/connecting-elixir-nodes-with-libcluster-locally-and-on-kubernetes/ Tr ...
- is_null, empty, isset的区别
参考博客: http://www.jb51.net/article/24666.htm http://www.jb51.net/article/36223.htm is_null, empty, is ...
- FastAdmin 生产环境升级注意
FastAdmin 生产环境升级注意 列出 FastAdmin 实际生产中升级注意事项. 安全相关,看 FastAdmin 的资讯. 如果使用 Git 更新生产文件,注意更新后的权限. JS 修改后注 ...
- hadoop 常见 命令
一 hadoop namenode 命令 1 格式化namanode 磁盘 hadoop namenode -format 二 hadoop fs 命令 和 linux 命令 非常类似 ...
- C# 文件上传和下载
一. 在Form中一定要将encType设为"multipart/form-data": <form id="WebForm3" method=&qu ...
- Linux split命令参数及用法详解---linux分割文件命令
转载自:http://blog.csdn.net/xiaoshunzi111/article/details/52173994 功能说明:分割文件. Split:按指定的行数截断文件 格式: spli ...