Setting System Date and Time

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Hi i have been trying to set the system date and time from a VB.NET (4.5 Framework), program and just seem to be getting errors, the code i use is

VB.NET:
        Dim da As Date = #12/10/2012#
        DateAndTime.Today = da.ToShortDateString

and it says my security levels are not correct (i am logged in to Win 7 with an administrator account).

I have searched the net and tried some things to change the security permissions but have had no luck, does anyone have any links they could share or some code to allow this operation.

This was so easy in XP and VB 6 LOL

Thanks

Stu
 
Hi,

I did not know the answer to this myself but I figured it out with a fully working function in less that 10 minutes. So, have another go it yourself. What you are looking for is the definition of the SYSTEMTIME Structure and then using that structure with the SetLocalTime Method of Kernel32.dll API.

Good luck and post back if you get stuck.

Cheers,

Ian
 
With administrator account you still have to explicitly request elevated rights to preform admin tasks. To test this right click the .exe and select "run as administrator".
An admin application can change application manifest to include "requireAdministrator" to always enter this mode. (project properties, Application page, 'View Windows Settings').

And btw, in this case don't listen to IanRyder, he's referring to the internal code for Today property.
 
Thanks guys I will have another look after work, this evening

Stu

Sent from my GT-I9300 using Tapatalk 4 Beta
 
There a small error in your code also, Today property is type Date, while you convert your Date value to a String value and assign that. This is forcing the compiler to implicitly convert it back to a Date value. Just assign the Date value. I recommend you turn on Option Strict, the compiler will then help you write Type correct code.
 
I have been looking at this again for ages and have tried the following:

VB.NET:
Imports System.Runtime.InteropServices
Public Class Form1
    <StructLayout(LayoutKind.Sequential)> Private Structure SYSTEMTIME
        <MarshalAs(UnmanagedType.U2)> Public Year As Short
        <MarshalAs(UnmanagedType.U2)> Public Month As Short
        <MarshalAs(UnmanagedType.U2)> Public DayOfWeek As Short
        <MarshalAs(UnmanagedType.U2)> Public Day As Short
        <MarshalAs(UnmanagedType.U2)> Public Hour As Short
        <MarshalAs(UnmanagedType.U2)> Public Minute As Short
        <MarshalAs(UnmanagedType.U2)> Public Second As Short
        <MarshalAs(UnmanagedType.U2)> Public Milliseconds As Short
    End Structure
    <DllImport("Kernel32.dll")> Private Shared Function SetLocalTime(ByRef time As SYSTEMTIME) As Boolean

    End Function

    Public Function UpdateSystemTime(ByVal DateObject As Date) 'As Boolean
        Dim newDT As SYSTEMTIME
        'Populate the systemtime object with the information from the date object passed in
        With newDT
            .Year = DateObject.Year
            .Month = DateObject.Month
            .DayOfWeek = DateObject.DayOfWeek
            .Day = DateObject.Day
            .Hour = DateObject.Hour
            .Minute = DateObject.Minute
            .Second = DateObject.Second
            .Milliseconds = DateObject.Millisecond
        End With

        Dim blnResult As Boolean = SetLocalTime(newDT)

        Return blnResult
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        UpdateSystemTime(#8/13/2002 12:14:00 PM#)
    End Sub

End Class

but it seems to do nothing, i did add a debug.print(blnResult) just before the Return blnResult statement but nothing it printed in the immediate window, next i put a breakpoint onto the line With newDT inside the UpdateSystemTime function and the program runs through the code but when i hold my mouse over the blnResult it had returned false.

Any more pointers?

Thanks

Stu
 
Use the Today property as you did first. Reimplementing the native calls only wastes your time.
 
Thanks guys and especially JohnH, all i needed was:

VB.NET:
        Dim da As Date = #10/6/2011 8:11:44 PM#
        DateAndTime.Today = da  ' change the date
        DateAndTime.TimeOfDay = da ' change the time

and

VB.NET:
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

in the app.manifest, i can not believe how long this has taken me, but happy now
 
Back
Top