How to change system date and time with vb.net on windows 7

8.4k views Asked by At

So I was trying to create a tiny program to rapidly change time on a windows 7. Following the "SetSystemTime(ByRef theDateTime As SYSTEMTIME)" like everyone does on the internet I did the same.

But nothing happened.

Here's the code:

Imports System.Data
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1

Private WithEvents timer1 As New Timer

<StructLayoutAttribute(LayoutKind.Sequential)> _
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", setLastError:=True)> _
Private Shared Function SetSystemTime(ByRef theDateTime As SYSTEMTIME) As Boolean

End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim newtime As New SYSTEMTIME

    newtime.year = dtpDate1.Value.Year
    newtime.month = dtpDate1.Value.Year
    newtime.dayOfWeek = dtpDate1.Value.DayOfWeek
    newtime.day = dtpDate1.Value.Day
    newtime.hour = CShort(txtHrs.Text) 'dtpDate1.Value.Hour
    newtime.minute = CShort(txtMins.Text) 'dtpDate1.Value.Minute
    newtime.second = CShort(txtSec.Text) 'dtpDate1.Value.Second
    newtime.milliseconds = dtpDate1.Value.Millisecond

    Try
        If SetSystemTime(newtime) Then
            MsgBox("time changed")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Try


        dtpDate1.Value = DateTime.Now
        dtpDate2.Value = DateTime.Now

        txtHrs.Text = DateTime.Now.Hour
        txtMins.Text = DateTime.Now.Minute
        txtSec.Text = DateTime.Now.Second

        txtHrst.Text = DateTime.Now.Hour
        txtMinst.Text = DateTime.Now.Minute
        txtSect.Text = DateTime.Now.Second

        timer1.Interval = 1000
        timer1.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    Dim newtime As New SYSTEMTIME

    newtime.year = dtpDate2.Value.Year
    newtime.month = dtpDate2.Value.Year
    newtime.dayOfWeek = dtpDate2.Value.DayOfWeek
    newtime.day = dtpDate1.Value.Day
    newtime.hour = CShort(txtHrst.Text) 'dtpDate1.Value.Hour
    newtime.minute = CShort(txtMinst.Text) 'dtpDate1.Value.Minute
    newtime.second = CShort(txtSect.Text) 'dtpDate1.Value.Second
    newtime.milliseconds = dtpDate2.Value.Millisecond

    Try
        If SetSystemTime(newtime) Then
            MsgBox("time changed")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Private Sub timer1_Tick(sender As Object, e As System.EventArgs) Handles timer1.Tick

    txtSec.Text = CInt(txtSec.Text) + 1
    txtSect.Text = CInt(txtSect.Text) + 1

End Sub

Private Sub txtSec_TextChanged(sender As Object, e As System.EventArgs) Handles txtSec.TextChanged

    If CInt(txtSec.Text) = 60 Then
        txtMins.Text = CInt(txtMins.Text) + 1
        txtSec.Text = 0
    End If

End Sub

Private Sub txtSect_TextChanged(sender As Object, e As System.EventArgs) Handles txtSect.TextChanged

    If CInt(txtSect.Text) = 60 Then
        txtMinst.Text = CInt(txtMinst.Text) + 1
        txtSect.Text = 0
    End If

End Sub


Private Sub txtMins_TextChanged(sender As Object, e As System.EventArgs) Handles txtMins.TextChanged

    If CInt(txtMins.Text) = 60 Then
        txtHrs.Text = CInt(txtHrs.Text) + 1
        txtMins.Text = 0
    End If

End Sub

Private Sub txtMinst_TextChanged(sender As Object, e As System.EventArgs) Handles txtMinst.TextChanged

    If CInt(txtMinst.Text) = 60 Then
        txtHrst.Text = CInt(txtHrst.Text) + 1
        txtMinst.Text = 0
    End If

End Sub

Private Sub txtHrs_TextChanged(sender As Object, e As System.EventArgs) Handles txtHrs.TextChanged

    If CInt(txtHrs.Text) = 24 Then
        dtpDate1.Value.Date.AddDays(1)
        txtHrs.Text = 0
    End If

End Sub

Private Sub txtHrst_TextChanged(sender As Object, e As System.EventArgs) Handles txtHrst.TextChanged

    If CInt(txtHrst.Text) = 24 Then
        dtpDate2.Value.Date.AddDays(1)
        txtHrst.Text = 0
    End If

End Sub
End Class

I don't know what I'm doing wrong and I tried to build an exe and run it as administrator.

3

There are 3 answers

1
Matt Wilko On BEST ANSWER

By far the easiest way to do this is to use a built in method in the Microsoft.VisualBasic namespace:

    'create a date to use
    Dim d As DateTime
    'set the date to a hour from now
    d = DateTime.Now.AddHours(1)
    'set the system date and time to this date and time - throw an exception if we can't set it
    Try
        Microsoft.VisualBasic.TimeOfDay = d
    Catch ex As Exception
        MessageBox.Show("Could not set the time. You probably need to run as Administrator to do this. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    End Try

Note that you usually need to be running as admin in order to change the system date or time.

2
Trevor On

You might not have the privilege to do so...

The calling process must have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege before changing the local time and disables the privilege before returning.

More on this here...

http://msdn.microsoft.com/en-us/library/windows/desktop/ms717802(v=vs.85).aspx

0
pc43 On

Simply use this code

Today = "06/09/2015 06:09:06 PM"