Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys, I need a code to update system time to internet time every time my application windows login form loads. If this is not possible, I'll like a way to programmatically turn on the auto update datetime windows feature if its turned off.

What I have tried:

I have searched the internet for codes but not found anything specific to my requirements. Help guys, please...
Posted
Updated 12-Jan-17 1:05am
Comments
Philippe Mori 12-Jan-17 13:58pm    
You should never do that. You are clearly abusing user system by playing with system time. You have no right to do so. People should not buy your software... as you don't respect them.
Akinloluwa 12-Jan-17 14:07pm    
I'm not trying to sell the program...lol. :) if for my personal use... thank you...
Philippe Mori 12-Jan-17 15:23pm    
Why peoples want to bypass any security features Microsoft has added since Vista...
Akinloluwa 13-Jan-17 6:07am    
lol...

 
Share this answer
 
Setting the time requires elevated privileges.

When using SetSystemTime as suggested by OriginalGriff, your application must have the SE_SYSTEMTIME_NAME privilege which is not set for non-administrative accounts.

Other methods like changing the automatic time update configuration require administrative privileges. An example is the command line tool to configure the time service: Windows Time Service Tools and Settings: Windows Time Service[^].

So from the security point of view, the best option is to use SetSystemTime and give your application only the SE_SYSTEMTIME_NAME privilege rather full adminstrative privileges.

An even better option would be letting your application check if the actual time is correct by comparing with data from a net time service. You can than inform the user if the difference is too large asking for setting the time (or just enable the Windows time service).
 
Share this answer
 
Comments
Akinloluwa 12-Jan-17 7:10am    
This application will be run with admin privileges. But your idea is also great. I can work with that... comparing both times... Please can I get a c# code to that :)
Jochen Arndt 12-Jan-17 7:30am    
You already got the necessary code from the links in the other solutions:
The link from solution 1 provides code on how to get the time from the internet
The link from solution 2 explains how to get and set the Windows time.

To get a time difference you have to convert the times to a format that allows subtraction (e.g. the C# DateTime type).

Creating DateTime objects from the values and calling the Subtract() method should be no problem. You will the get the result as a TimeSpan object.
Akinloluwa 12-Jan-17 14:06pm    
thank you... really helpful...
Hi Akinloluwa.

There is a solution to this type of a problem here is a link to the solution.

Hope it helps. cheers.
 
Share this answer
 
Comments
Akinloluwa 12-Jan-17 7:04am    
Thank you... but this code does not set the time. Please can guide me on how to use the returned value to update the system time... Thank you...

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