如何使用DateTimePicker到SetSystemTime?

时间:2011-01-05 18:28:02

标签: c# winforms compact-framework datetimepicker

这可能很简单,但在Microsoft Visual Studio Microsoft.NET WinForms CompactFramework v2.0 Windows CE 5.0中,我没有看到DateTimePicker属性或方法可用于SetSystemTime。< / p>

修改:更具体地说,如何从DateTimePicker中获取所选日期,以便将其应用于SetSystemTime

2 个答案:

答案 0 :(得分:3)

我认为以下代码段应该有效:

[DllImport("coredll.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;

    public SYSTEMTIME(DateTime value)
    {
        wYear = value.Year;
        wMonth = value.Month;
        wDayOfWeek = value.DayOfWeek;
        wDay = value.Day;
        wHour = value.Hour;
        wMinute = value.Minute;
        wSecond = value.Second;
        wMilliseconds = value.Milliseconds;
    }
}

public void setTimeButton_Click(object sender, EventArgs e)
{
    // DateTimePicker usually provide with the date but not time information
    // so we need to get the current time
    TimeSpan currentSystemTime = DateTime.Now.TimeOfDay;
    DateTime newDate = newDateTimePicker.Value.Date;
    // Join the date and time parts
    DateTime newDateTime = newDate.Add(currentSystemTime);

    SYSTEMTIME newSystemTime = new SYSTEMTIME(newDateTime);
    if (!SetSystemTime(newSystemTime))
        Debug.WriteLine("Error setting system time.");
}

答案 1 :(得分:2)