Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to change my computer date and time from my code ...
so , I googled a lot and I found this code , but it didn't work correctly . when I've called "SetLocalTime()" it didn't change my system date and time and it returned the "false" value..
I don't Know why it doesn't work ..plz help me ...
VB
Imports System.Runtime.InteropServices

Public Class FrmSetDateTime

    'System time structure used to pass to P/Invoke...
    <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

    'P/Invoke dec for setting the system time...
    <dllimport("kernel32.dll",> _
    Private Shared Function SetLocalTime(ByRef time As SYSTEMTIME) As Boolean

    End Function
    Public Sub SetDeviceTime(ByVal p_NewDate As Date)
        'Populate structure...
        'Substitute <your date="" object=""> with your date object returned via GPRS...

        Dim st As SYSTEMTIME
        st.year = p_NewDate.Year
        st.month = p_NewDate.Month
        st.dayOfWeek = p_NewDate.DayOfWeek
        st.day = p_NewDate.Day
        st.hour = p_NewDate.Hour
        st.minute = p_NewDate.Minute
        st.second = p_NewDate.Second
        st.milliseconds = p_NewDate.Millisecond

        'Set the new time...
        Dim bool As Boolean = False

        bool = SetLocalTime(st)

        If bool = True Then

        End If
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click

        Dim DateNew As DateTime
        Dim Arr As String() = TxtTime.Text.Split(":")

        DateNew = ARECO._ConvertDate.ShamsiToMiladi(DateSet.Text, True)
        Dim ts As TimeSpan = New TimeSpan(Arr(0), Arr(1), Arr(2))
        DateNew = DateNew + ts

        SetDeviceTime(DateNew)
    End Sub
 End Class
Posted
v2

1 solution

You probably need to run in privileged mode. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724936(v=vs.85).aspx[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900