是否可以在不更改小时、分钟和秒(仅年、月和日)的情况下设置系统时间(使用Win32SetSystemTime
)?
这是我的代码。如您所见,我评论了 falseData.Hour、falseData.Minute 和 falseData.Second,因为我不想更改它们。但是,使用此代码,系统时间会自动设置为 01:00:00。手动,我可以更改系统日期而不更改小时、分钟和秒(通过单击任务栏的时钟),但是以编程方式?
任何帮助将不胜感激。谢谢。马可
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.InteropServices;
namespace SysData1_0
{
public partial class Form1 : Form
{
public struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
};
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
public extern static void Win32GetSystemTime(ref SystemTime sysTime);
[DllImport("kernel32.dll", EntryPoint ="SetSystemTime",SetLastError=true)]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
public Form1()
{
InitializeComponent();
}
private void buttonImpostaDataFittizia_Click(object sender, EventArgs e)
{
SystemTime falseData = new SystemTime ();
if (comboBox.SelectedIndex == 0)
{
falseData.Year = (ushort)2015;
falseData.Month = (ushort)3;
}
if (comboBox.SelectedIndex == 1)
{
falseData.Year = (ushort)2014;
falseData.Month = (ushort)9;
}
//Please, read here
falseData.Day = (ushort)1;
//falseData.Hour = (ushort)10 - 1;
//falseData.Minute = (ushort)30;
//falseData.Second = (ushort)30;
Win32SetSystemTime(ref falseData);
string Y,M,D;
Y = Convert.ToString(falseData.Year);
M = Convert.ToString(falseData.Month);
D = Convert.ToString(falseData.Day);
textBoxDataImpostata.Text = D + "/" + M + "/" + Y;
}
}
}