I got this code from this link here:"http://bytes.com/topic/c-sharp/answers/474927-problem-setting-time-zone-settimezoneinfo rmation". There is a problem with this line : object varValue = rkTZInfo.GetVal ue("TZI"); which is rkTZInfo needs to be instantiated and I can't see what to instantiate it to.
Code:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool SetTimeZoneInformation(ref
TIME_ZONE_INFORMATION lpTimeZoneInformation);
[StructLayoutAttribute(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct TIME_ZONE_INFORMATION
{
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string StandardName;
public SYSTEMTIME StandardDate;
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DaylightName;
public SYSTEMTIME DaylightDate;
public int DaylightBias;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
private struct REGTZI
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}
public bool SetTimeZone(int index)
{
// Omitted (uses index to find the time zone in the registry so that I can get the
// information about the time zone using the appropriate key's TZI value
// We've found the right time zone, get the TZI data
object varValue = rkTZInfo.GetValue("TZI");
byte[] baData = varValue as byte[];
int iSize = baData.Length;
IntPtr buffer = Marshal.AllocHGlobal(iSize);
Marshal.Copy(baData, 0, buffer, iSize);
REGTZI rtzi;
rtzi = (REGTZI)Marshal.PtrToStructure(buffer, typeof(REGTZI));
Marshal.FreeHGlobal(buffer);
// Now fill out TIME_ZONE_INFORMATION with that data
TIME_ZONE_INFORMATION tZoneInfo = new TIME_ZONE_INFORMATION();
tZoneInfo.Bias = rtzi.Bias;
tZoneInfo.StandardBias = rtzi.StandardBias;
tZoneInfo.DaylightBias = rtzi.DaylightBias;
tZoneInfo.StandardDate = rtzi.StandardDate;
tZoneInfo.DaylightDate = rtzi.DaylightDate;
tZoneInfo.StandardName = (string)rkTZInfo.GetValue("Std");
tZoneInfo.DaylightName = (string)rkTZInfo.GetValue("Dlt");
if (!SetTimeZoneInformation(ref tZoneInfo))
{
return (false);
}
}
Comment