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
Hi. New to the site so hopefully I follow the rules :-) My questions is how do I successfully change the system time? The above code I found on this site (as well as much info on other parts of my project - thank you!) and there are no errors, but it always throws an exception. I run as admin and I've tried changing the UAC and I still can't change the time. I read that I need to have the SE_SYSTEMTIME_NAME privilege set, but I have this set so that all users (ie me) have the right, still nothing. The MS reference here does not provide much insight. I suspect it's an issue with privileges, but i can't seem to see how to set what i need. What do I need to do to allow my application to change the system time to a value?
More info...there is another question along the same lines, but it's c# not Vb and I've tried something similar to that code, which is below. Still
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