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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
There's another way to do this, which admittedly is a bit of a hack, but works quite well in practice:
Simply call this method and pass the
TimeZoneInfo.Id
that you wish to set. For example:No special privileges are required to run this code, as
tzutil.exe
already has the appropriate permissions.There is an article for doing this using VB on Microsoft's support site:
How to change time zone information by using Visual Basic
I don't know if this is what you are looking for, but there is some information on how to use the Win32 functions to get and set the timezone information at
http://www.pinvoke.net/default.aspx/kernel32/GetTimeZoneInformation.html
The main part of the following sample code is taken from this site. I just added a few more lines to actually call the GetTimeZone() method and fixed a small error in the access modifier of the SystemTime struct to make the sample work.
Hope this helps...
Before setting the time zone information you will need to ensure that the process has the appropriate privileges. As noted on the MSDN page for SetTimeZoneInformation:
Here is some sample code to enable this privilege: