Question

How do you set the Windows time zone on the local machine programmatically in C#? Using an interactive tool is not an option because the remote units have no user interface or users. The remote machine is running .NET 2.0 and Windows XP Embedded and a local app that communicates with a central server (via web service) for automated direction of its tasks. We can deliver a command to synch to a certain time/zone combination, but what code can be put in the local app to accomplish the change? The equipment is not imaged for specific locations before installation, so in order to use any equipment at any location, we have to be able to synch this information.

Solution

SetTimeZoneInformation should do what you need. You'll need to use P/Invoke to get at it.

Note also that you'll need to possess and enable the SE_TIME_ZONE_NAME privilege.

OTHER TIPS

This is not working in Windows 7. I have tried with the following environment windows 7 VSTS-2008 It is opening Change Time Zone Window as we do it manually

santosh you are definitly correct. RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1 is deprecated for years and will neither run on Windows 2008, R2, Vista, 7, ... oh, yes i noticed that this forum seems to have a complete bug with recognizing the enter key. maybe someday the programmer will fix that problem.

Try this...

First, you need to find, in the registry, the key that represents the zone you want ("Central Standard Time" is an example). Those are located here:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Current Version\Time Zones

So, with that in mind, create a batch file, "SetTimeZone.bat" with the following line in it:

RunDLL32 shell32.dll,Control_RunDLL %SystemRoot%\system32\TIMEDATE.cpl,,/Z %1

From C#, call:

System.Diagnostics.Process.Start("SetTimeZone.bat", "The key of the time zone you want to set");

You will run the risk of having incorrect data if you do not use UTC to transmit dates... If you change time zones on the device... you're dates will be even further off.

You may want to use UTC and then calculate time in each timezone.

Instead of just setting the time zone of some systems you should consider to use the Windows Time Service. It will not only handle the time zone. It will also take care about correct settings of date and time itself.

Take a look at: How to synchronize the time with the Windows Time service in Windows XP

Even if you maybe going to make all these settings within Vista or Windows 7 take a look at this here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow