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参考这里没有提供太多的洞察力。我怀疑这是一个特权问题,但我似乎看不出如何设置我所需要的。要允许应用程序将系统时间更改为值,需要做什么?
更多的info...there是另一个类似的问题,但它是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发布于 2016-05-02 03:16:52
正如注释中提到的那样,这本质上是这个问题的重复。但要澄清VB.NET反对C#的观点,请回答问题中的一个问题:
在Windows,7,8操作系统上,这将需要一个UAC提示,以便获得成功执行SetSystemTime功能所需的管理权限。 原因是调用进程需要SE_SYSTEMTIME_NAME特权。SetSystemTime函数期望在协调通用时间(UTC)中使用SYSTEMTIME结构。否则,它将无法正常工作。 根据获取DateTime值的位置/方式,在设置SYSTEMTIME中相应的值之前,最好安全地使用ToUniversalTime()。
代码示例(为VB.NET修改):
Dim tempDateTime As DateTime = GetDateTimeFromSomeService()
Dim dateTime As DateTime = tempDateTime.ToUniversalTime()
Dim st As SYSTEMTIME
'All of these must be short
st.wYear = dateTime.Year.ToInt16()
st.wMonth = dateTime.Month.ToInt16()
st.wDay = dateTime.Day.ToInt16()
st.wHour = dateTime.Hour.ToInt16()
st.wMinute = dateTime.Minute.ToInt16()
st.wSecond = dateTime.Second.ToInt16()
// invoke the SetSystemTime method now
SetSystemTime(ByRef st)是的,你需要管理员特权。
https://stackoverflow.com/questions/36967493
复制相似问题