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

时间:2015-02-18 17:42:16

标签: vb.net datetime

我尝试使用下面的代码示例更改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的结果时,它返回false并且系统的日期时间不会改变。我也尝试过SetSystemTime函数,但它返回0并且也没有改变任何东西。我在Windows上使用管理员帐户。

我应该修改什么来使这项工作?

2 个答案:

答案 0 :(得分:3)

你不需要做任何你想做的事情。您可以使用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

答案 1 :(得分:0)

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