Does anyone have some code that will take a TimeZoneInfo field from .NET and execute the interop code to set the system time zone via SetTimeZoneInformation? I realize that it's basically mapping the TimeZoneInfo members to the struct members, but it does not appear obvious to me how the fields will map exactly or what I should care about beyond the bias.
DateTime itself contains no real timezone information.
C# TimeZone ExamplesUse the TimeZone type, which is a class in the System namespace that represents different time zones. TimeZone. This C# type, part of System, describes the current time zone. It gets information about the time zone of the current computer. Notes, time zones.
There's another way to do this, which admittedly is a bit of a hack, but works quite well in practice:
public void SetSystemTimeZone(string timeZoneId)
{
var process = Process.Start(new ProcessStartInfo
{
FileName = "tzutil.exe",
Arguments = "/s \"" + timeZoneId + "\"",
UseShellExecute = false,
CreateNoWindow = true
});
if (process != null)
{
process.WaitForExit();
TimeZoneInfo.ClearCachedData();
}
}
Simply call this method and pass the TimeZoneInfo.Id
that you wish to set. For example:
SetSystemTimeZone("Eastern Standard Time");
No special privileges are required to run this code, as tzutil.exe
already has the appropriate permissions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With