VB.NET更改Windows系统日期时间 [英] VB.NET change windows system date time
问题描述
我正在尝试使用下面的代码示例来更改Windows 8系统日期时间。
I'm trying to change the Windows 8 system datetime with the code example below.
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,系统的datetime不会更改。我也尝试过SetSystemTime函数,但它返回0,也不会改变任何东西。我在Windows上使用管理员帐号。
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.
我应该修改什么来使这项工作?
What should I modify to make this work?
推荐答案
你不需要任何这样做你想要的。您可以使用 Microsoft.VisualBasic
命名空间来实现此目的。以下是您需要的简短示例。
You don't need any of that to do what you want. You can use Microsoft.VisualBasic
namespace to accomplish this. Here's a short example for what you need. This is tried and tested as well.
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
这篇关于VB.NET更改Windows系统日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!