Re: Change system date

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Crouchie1998 (Crouchie1998_at_discussions.microsoft.com)
Date: 02/15/05


Date: Mon, 14 Feb 2005 19:13:01 -0800

Here's a VB.NET solution to change system time:

Imports System.Runtime.InteropServices

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Structure

    Private Declare Function SetSystemTime Lib "kernel32" (ByRef
lpSystemTime As SYSTEMTIME) As Integer
    Private Declare Sub GetSystemTime Lib "kernel32" Alias "GetSystemTime"
(ByRef lpSystemTime As SYSTEMTIME)

Usage (SetSystemTime):

        Dim st As SYSTEMTIME
        With st
            .wDay = 1
            .wMonth = 1
            .wYear = 1981
            .wMinute = 1
            .wHour = 1
            .wSecond = 0
        End With
        If SetSystemTime(st) = 1 Then
            MessageBox.Show("System Time Changed")
        Else
            MessageBox.Show(Marshal.GetLastWin32Error.ToString)
        End If

Usage (GetSystemTime):

        Dim sysTime As SYSTEMTIME
        GetSystemTime(sysTime)
        MessageBox.Show(sysTime.wHour.ToString())

Obviously, I am running under administration rights & you need to take this
into account if you are just a user.

I hope this helps



Relevant Pages

  • Re: Any one know that how to do
    ... Public Structure SYSTEMTIME ... Public Declare Sub GetSystemTime _ ... (ByRef lpSystemTime As SYSTEMTIME) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Call Windows API from VB.Net?
    ... Public Structure SYSTEMTIME ... Public Declare Sub GetSystemTime _ ... Private Sub Form1_Load(ByVal sender _ ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Timestring
    ... If you use the SYSTEMTIME Structure you will be able to get milliseconds ... Public Structure SYSTEMTIME ... (ByRef lpSystemTime As SYSTEMTIME) ...
    (microsoft.public.dotnet.languages.vb)