VB.NET 更改 windows 系统日期时间

VB.NET change windows system date time

我正在尝试使用下面的代码示例更改 Windows 8 系统日期时间。

Private Structure SYSTEMTIME

    Public year As Short

    Public month As Short

    Public dayOfWeek As Short

    Public day As Short

    Public hour As Short

    Public minute As Short

    Public second As Short

    Public milliseconds As Short

End Structure

<DllImport("Kernel32.dll")> Private Shared Function SetLocalTime(ByRef time As SYSTEMTIME) As Boolean

End Function

Private Sub ChangeDate

            Dim st As SYSTEMTIME
            Dim NewDate As Date = "28-April-1978 22:30:00"
            st.year = NewDate .Year
            st.month = NewDate.Month
            st.dayOfWeek = NewDate.DayOfWeek
            st.day = NewDate.Day
            st.hour = NewDate.Hour
            st.minute = NewDate.Minute
            st.second = NewDate.Second
            st.milliseconds = NewDate.Millisecond

            'Set the new time...
            SetLocalTime(st)
End Sub

当我打印 SetLocalTime 的结果时,它 returns 错误并且系统的日期时间没有改变。我也尝试过 SetSystemTime 函数,但它 returns 0 也没有改变任何东西。我在 windows 上使用管理员帐户。

我应该修改什么才能使其正常工作?

你不需要任何这些来做你想做的事。您可以使用 Microsoft.VisualBasic 命名空间来完成此操作。这是您需要的一个简短示例。 这也经过了试验和测试

Private Sub ChangeDate()
   Dim d As DateTime
   d = "22:30:00"

   Try
      Microsoft.VisualBasic.TimeOfDay = d 'Your time...
      Microsoft.VisualBasic.DateString = New Date(1978, 4, 28) 'The date...
   Catch ex As Exception
      'You might have to run as Administrator...?
   End Try

End Sub
Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\International", "sShortDate", "dd/MMM/yyyy hh:mm:ss")