R
remarkpk
Guest
Hey everybody,
I'm trying to set the time zone with C# code... after I run the following code and I look at the time zone properties in control panel, the offset is indeed accurate but the time zone display name is "Unknown Time Zone" and not the one that I have set. Here's what I've got: (I'm trying to set my time zone to MST, Mountain Standard Time). Also, there is a piece of code below that sets the privilege for my application to set the time zone, just assume that it works fine Why doesn't the OS recognize the name of the time zone? What am I doing wrong? Thanks in advance.
Code Snippet
public class Time
{
#region
DLL Imports
[
DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(out TimeZoneInformation lpTimeZoneInformation);
[
DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref TimeZoneInformation lpTimeZoneInformation);
[
StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[
StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
public int bias;
[
MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
[
MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
SYSTEMTIME standardDate;
SYSTEMTIME daylightDate;
public int standardBias;
public int daylightBias;
}
#endregion
public static TimeZone CurrentTimeZone
{
get { return TimeZone.CurrentTimeZone; }
}
{
ComputerManager.EnableToken("SeTimeZonePrivilege", Process.GetCurrentProcess().Handle);
// set local system timezone
return SetTimeZoneInformation(ref tzi);
}
public static TimeZoneCollection GetTimeZones()
{
//open key where all time zones are located in the registry
RegistryKey timeZoneKeys = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
//create a new hashtable which will store the name
//of the timezone and the associate time zone information struct
TimeZoneCollection zones = new TimeZoneCollection();
//iterate through each time zone in the registry and add it to the hash table
foreach (string zonekey in timeZoneKeys.GetSubKeyNames())
{
//get current time zone key
RegistryKey individualZone = timeZoneKeys.OpenSubKey(zonekey);
//create new TZI struct and populate it with values from key
TimeZoneInformation TZI = new TimeZoneInformation();
TZI.standardName = individualZone.GetValue(
"Dlt").ToString();
TZI.daylightName = individualZone.GetValue(
"Display").ToString();
//read binary TZI data, convert to byte array
byte[] b = (byte[])individualZone.GetValue("TZI");
TZI.bias =
BitConverter.ToInt32(b, 0);
//add the name and TZI struct to hash table
zones.Add(TZI.daylightName, TZI);
}
return zones;
}
}
static void Main(string[] args)
{
Time.TimeZoneInformation tzi = new Time.TimeZoneInformation();
string name = string.Empty;
foreach (string key in Time.GetTimeZones().Keys)
{
if (key.Contains("Mountain"))
{
name = key;
}
}
tzi = (
Time.TimeZoneInformation)Time.GetTimeZones()[name];
Time.SetTimeZone(tzi);
}
Continue reading...
I'm trying to set the time zone with C# code... after I run the following code and I look at the time zone properties in control panel, the offset is indeed accurate but the time zone display name is "Unknown Time Zone" and not the one that I have set. Here's what I've got: (I'm trying to set my time zone to MST, Mountain Standard Time). Also, there is a piece of code below that sets the privilege for my application to set the time zone, just assume that it works fine Why doesn't the OS recognize the name of the time zone? What am I doing wrong? Thanks in advance.
Code Snippet
public class Time
{
#region
DLL Imports
[
DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(out TimeZoneInformation lpTimeZoneInformation);
[
DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref TimeZoneInformation lpTimeZoneInformation);
[
StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[
StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
public int bias;
[
MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
[
MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
SYSTEMTIME standardDate;
SYSTEMTIME daylightDate;
public int standardBias;
public int daylightBias;
}
#endregion
public static TimeZone CurrentTimeZone
{
get { return TimeZone.CurrentTimeZone; }
}
public static bool SetTimeZone(TimeZoneInformation tzi)
{
ComputerManager.EnableToken("SeTimeZonePrivilege", Process.GetCurrentProcess().Handle);
// set local system timezone
return SetTimeZoneInformation(ref tzi);
}
public static TimeZoneCollection GetTimeZones()
{
//open key where all time zones are located in the registry
RegistryKey timeZoneKeys = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
//create a new hashtable which will store the name
//of the timezone and the associate time zone information struct
TimeZoneCollection zones = new TimeZoneCollection();
//iterate through each time zone in the registry and add it to the hash table
foreach (string zonekey in timeZoneKeys.GetSubKeyNames())
{
//get current time zone key
RegistryKey individualZone = timeZoneKeys.OpenSubKey(zonekey);
//create new TZI struct and populate it with values from key
TimeZoneInformation TZI = new TimeZoneInformation();
TZI.standardName = individualZone.GetValue(
"Dlt").ToString();
TZI.daylightName = individualZone.GetValue(
"Display").ToString();
//read binary TZI data, convert to byte array
byte[] b = (byte[])individualZone.GetValue("TZI");
TZI.bias =
BitConverter.ToInt32(b, 0);
//add the name and TZI struct to hash table
zones.Add(TZI.daylightName, TZI);
}
return zones;
}
}
static void Main(string[] args)
{
Time.TimeZoneInformation tzi = new Time.TimeZoneInformation();
string name = string.Empty;
foreach (string key in Time.GetTimeZones().Keys)
{
if (key.Contains("Mountain"))
{
name = key;
}
}
tzi = (
Time.TimeZoneInformation)Time.GetTimeZones()[name];
Time.SetTimeZone(tzi);
}
Continue reading...