这可能很简单,但是在Microsoft Visual Studio Microsoft.NET WinForms CompactFramework v2.0 Windows CE 5.0中看不到可以用于DateTimePickerSetSystemTime属性或方法。

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

最佳答案

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

[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.");
}

关于c# - 如何使用DateTimePicker设置SetSystemTime?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4607764/

10-12 15:27