【发布时间】:2016-05-01 13:58:53
【问题描述】:
Sub change_the_time(ByVal NewDateTime As DateTime)
'
Dim NewDateTime2 As DateTime
'
NewDateTime2 = #5/1/2016 5:52:15 PM# ' try setting the time to this
'
'set the system date and time to this date and time - throw an exception if it can't
Try
TimeOfDay = NewDateTime2
Catch ex As Exception
MessageBox.Show("Could not set time. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub
嗨。 新网站所以希望我遵守规则:-) 我的问题是如何成功更改系统时间?我在这个网站上找到的上述代码(以及我项目其他部分的大量信息 - 谢谢!)并且没有错误,但它总是抛出异常。我以管理员身份运行,我尝试更改 UAC,但仍然无法更改时间。我读到我需要设置 SE_SYSTEMTIME_NAME 权限,但我设置了这个权限,以便所有用户(即我)都有权限,但仍然没有。 MS 参考here 没有提供太多见解。我怀疑这是特权问题,但我似乎看不到如何设置我需要的东西。我需要做什么才能让我的应用程序将系统时间更改为某个值?
更多信息...还有另一个问题,但它是 c# 而不是 Vb,我尝试了类似于该代码的东西,如下所示。还是
Private Sub change_the_time2(ByRef NewDateTime As DateTime)
Dim d As DateTime
d = #6/10/2011# ' try setting the date to this before using NewDateTime
Dim worked As Boolean
'
Try
worked = setlocaltime(d)
MsgBox(" 1. Did it work " & worked)
Catch ex As Exception
MsgBox(" 2. Did it work " & worked)
End Try
End Sub
<DllImport("kernel32.dll", setLastError:=True)> _
Private Shared Function setlocaltime(ByRef time As System.DateTime) As Boolean
End Function
【问题讨论】:
-
有什么异常?
-
您好 SuperPeanut,“无法设置时间。安全权限不足,无法设置系统时间”
-
你应该不需要以管理员身份运行吗?
标签: vb.net