110,538
社区成员
发帖
与我相关
我的任务
分享
//api函数声明
[dllimport ("kernel32.dll", charset=charset.ansi)]
public extern static bool setsystemtime(ref systemtime time);
private void button1_click(object sender, system.eventargs e)
{
//调用代码
systemtime t = new systemtime ();
t.year = 2000;
t.month = 1;
t.day = 2;
t.hour = 12-8; //这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8
t.minute = 5;
bool v = setsystemtime(ref t);
console.writeline(v.tostring());
}
//引用
using System.Runtime.InteropServices;
//api函数声明
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);
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 Milliseconds;
}
private void Setdatetime()
{
//调用代码
DateTime idag = new DateTime((int)DateTime.Now.AddYears(1).Year,//增加年
(int)DateTime.Now.AddMonths(0).Month,//增加月
(int)DateTime.Now.AddDays(0).Day,//增加天
(int)(DateTime.Now.AddHours(0).Hour),//增加小时
(int)(DateTime.Now.AddMinutes(0).Minute),//增加分
(int)(DateTime.Now.AddSeconds(0).Second),//增加秒
DateTimeKind.Local);
SYSTEMTIME s = new SYSTEMTIME();
s.Year = (short)idag.Year;
s.Month = (short)idag.Month;
s.DayOfWeek = (short)idag.DayOfWeek;
s.Day = (short)idag.Day;
s.Hour = (short)idag.Hour;
s.Minute = (short)idag.Minute;
s.Second = (short)idag.Second;
s.Milliseconds = (short)idag.Millisecond;
bool v= SetSystemTime(ref s);
Console.WriteLine(v.ToString());
}