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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28590236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:49:13  来源:igfitidea点击:

VB.NET change windows system date time

vb.netdatetime

提问by fwan

I'm trying to change the Windows 8 system datetime with the code example below.

我正在尝试使用下面的代码示例更改 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

When I print the result of SetLocalTime, it returns false and the system's datetime does not change. I have tried SetSystemTime function also but it returns 0 and does not change anything too. I am using the admin account on windows.

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

What should I modify to make this work?

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

回答by ???ěxě?

You don't need any of that to do what you want. You can use Microsoft.VisualBasicnamespace to accomplish this. Here's a short example for what you need. This is tried and tested as well.

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

回答by Sandeep Suri

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