2
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
P_R
  • 21
  • 3

1 Answers1

1

This is essentially a duplicate of this question as has been mentioned in the comments. But to clarify for VB.NET as oposed to C#, per one of the answers in that question:

On Windows Vista, 7, 8 OS this will require a UAC Prompt in order to obtain the necessary administrative rights to successfully execute the SetSystemTime function.

The reason is that calling process needs the SE_SYSTEMTIME_NAME privilege. The SetSystemTime function is expecting a SYSTEMTIME struct in coordinated universal time (UTC). It will not work as desired otherwise.

Depending on where/ how you are getting your DateTime values, it might be best to play it safe and use ToUniversalTime() before setting the corresponding values in the SYSTEMTIME struct.

Code example (modified for 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)

Yes, you need Administrator privileges.

Community
  • 1
  • 1
  • Thank you! The error wasn't with the code, although thank you for the code above as it is more precise than anything I had. The code I had worked when run as admin. I thought I *was* running the code as admin, but a *proper* check showed me otherwise. Thank you again. – P_R May 02 '16 at 12:46