通过按钮单击将工作站更改为第二天或前一天

时间:2015-07-20 16:48:27

标签: c#

我正在开发一个WFA应用程序,它将计算机日期更改为提前一天或后一天,具体取决于按钮单击。这是我的代码:

public Form1()
        {
            InitializeComponent();
            currentTime.Start();
        }

        private void CurrentTime_Tick(object sender, EventArgs e)
        {
            DateTime dateTime = DateTime.Now;
            this.lblTime.Text = dateTime.ToString();
        }

        public struct Systemtime
        {
            public short WDay;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetSystemTime([In] ref Systemtime st);

        private void BtnChangeDate_Click(object sender, EventArgs e)
        {
            Systemtime st = new Systemtime();

            st.WDay++;

            SetSystemTime(ref st);
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnPreviousDay_Click(object sender, EventArgs e)
        {
            Systemtime st = new Systemtime();

            st.WDay--;

            SetSystemTime(ref st);
        }

我现在遇到的问题是按钮什么都不做,即使我有硬编码值。我需要做些什么才能正常工作?

2 个答案:

答案 0 :(得分:2)

在调用Systemtime函数之前,您需要正确设置SetSystemTime结构,仅仅设置WDay值是不够的。我创建了一种方法来执行以下两种方法。请注意,我使用内置方法来获取日期并添加/减去正确的天数。这可以处理所有情况,例如月/年的开始/结束。

private void ChangeDate(int numDays)
{
    Systemtime st = new Systemtime();

    var newDate = DateTime.Now.AddDays(numDays);

    //Set up all the values, no need to set wDayOfWeek as it's ignored by the set function
    st.wYear = newDate.Year;
    st.wMonth = newDate.Month;
    st.wDay = newDate.Day;
    st.wHour = newDate.Hour;
    st.wMinute = newDate.Minute;
    st.wSecond = newDate.Second;
    st.wMilliseconds = newDate.Millisecond;

    SetSystemTime(ref st);
}

现在你这样称呼它:

//Forward 1 day
ChangeDate(1);

//Back 1 day
ChangeDate(-1);

答案 1 :(得分:0)

这是解决方案。

public partial class Form1 : Form
{
    [DllImport("kernel32.dll")]
    static extern bool SetSystemTime([In] ref SYSTEMTIME st);

    public Form1()
    {
        InitializeComponent();
    }

    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;
        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;
        }
        public DateTime ToDateTime()
        {
            return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
        }
        public static DateTime ToDateTime(SYSTEMTIME time)
        {
            return time.ToDateTime();
        }
    }

    private void BtnChangeDate_Click(object sender, EventArgs e)
    {
        ChangeDateUp(0);
    }

    private void BtnPreviousDay_Click(object sender, EventArgs e)
    {
        ChangeDateDown(0);
    }
    private void ChangeDateUp(int numDays)
    {            
        DateTime t = DateTime.Now.AddDays(1);
        SYSTEMTIME st = new SYSTEMTIME();
        st.FromDateTime(t);
        SetSystemTime(ref st);
    }

    private void ChangeDateDown(int numDays)
    {
        DateTime t = DateTime.Now.AddDays(-1);
        SYSTEMTIME st = new SYSTEMTIME();
        st.FromDateTime(t);
        SetSystemTime(ref st);
    }

    private void BtnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}