How do I set the Windows system clock to the correct local time using C#?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You will need to P/Invoke the
SetLocalTimefunction from the Windows API. Declare it like this in C#:To set the time, you simply initialize an instance of the
SYSTEMTIMEstructure with the appropriate values, and call the function. Sample code:However, note that the calling process must have the appropriate privileges in order to call this function. In Windows Vista and later, this means you will have to request process elevation.
Alternatively, you could use the
SetSystemTimefunction, which allows you to set the time in UTC (Coordinated Universal Time). The sameSYSTEMTIMEstructure is used, and the two functions are called in an identical fashion.