EDN Admin
Well-known member
Hi All,
Below is the working C# code for setting system time zone.
This is working perfectly fine on Windows XP and Windows 7.
I need to know if this works on Windows Vista/ Windows Server 2008.
Can any one help me in this if you have Vista in your system?
Thanks,
Vinayak
===================================================
<br/>
using System; <br/>
using System.Collections.Generic; <br/>
using System.Linq; <br/>
using System.Text; <br/>
using System.Runtime.InteropServices; <br/>
using Microsoft.Win32;<br/>
using System.Security.AccessControl;
namespace TimeZoneTest<br/>
{<br/>
[StructLayoutAttribute(LayoutKind.Sequential)]<br/>
public struct SystemTime<br/>
{<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Year;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Month;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short DayOfWeek;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Day;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Hour;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Minute;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Second;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Milliseconds;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct TimeZoneInformation<br/>
{<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int Bias;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]<br/>
public string StandardName;<br/>
public SystemTime StandardDate;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int StandardBias;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]<br/>
public string DaylightName;<br/>
public SystemTime DaylightDate;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int DaylightBias;<br/>
}
[StructLayout(LayoutKind.Sequential)]<br/>
public struct RegistryTimeZoneInformation<br/>
{<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int Bias;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int StandardBias;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int DaylightBias;<br/>
public SystemTime StandardDate;<br/>
public SystemTime DaylightDate;
public RegistryTimeZoneInformation(TimeZoneInformation tzi)<br/>
{<br/>
this.Bias = tzi.Bias;<br/>
this.StandardDate = tzi.StandardDate;<br/>
this.StandardBias = tzi.StandardBias;<br/>
this.DaylightDate = tzi.DaylightDate;<br/>
this.DaylightBias = tzi.DaylightBias;<br/>
}
public RegistryTimeZoneInformation(byte[] bytes)<br/>
{<br/>
if ((bytes == null) || (bytes.Length != 0x2c))<br/>
{<br/>
throw new ArgumentException("Argument_InvalidREG_TZI_FORMAT");<br/>
}<br/>
this.Bias = BitConverter.ToInt32(bytes, 0);<br/>
this.StandardBias = BitConverter.ToInt32(bytes, 4);<br/>
this.DaylightBias = BitConverter.ToInt32(bytes, 8);<br/>
this.StandardDate.Year = BitConverter.ToInt16(bytes, 12);<br/>
this.StandardDate.Month = BitConverter.ToInt16(bytes, 14);<br/>
this.StandardDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x10);<br/>
this.StandardDate.Day = BitConverter.ToInt16(bytes, 0x12);<br/>
this.StandardDate.Hour = BitConverter.ToInt16(bytes, 20);<br/>
this.StandardDate.Minute = BitConverter.ToInt16(bytes, 0x16);<br/>
this.StandardDate.Second = BitConverter.ToInt16(bytes, 0x18);<br/>
this.StandardDate.Milliseconds = BitConverter.ToInt16(bytes, 0x1a);<br/>
this.DaylightDate.Year = BitConverter.ToInt16(bytes, 0x1c);<br/>
this.DaylightDate.Month = BitConverter.ToInt16(bytes, 30);<br/>
this.DaylightDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x20);<br/>
this.DaylightDate.Day = BitConverter.ToInt16(bytes, 0x22);<br/>
this.DaylightDate.Hour = BitConverter.ToInt16(bytes, 0x24);<br/>
this.DaylightDate.Minute = BitConverter.ToInt16(bytes, 0x26);<br/>
this.DaylightDate.Second = BitConverter.ToInt16(bytes, 40);<br/>
this.DaylightDate.Milliseconds = BitConverter.ToInt16(bytes, 0x2a);<br/>
}<br/>
}
public class TokenPrivilegesAccess<br/>
{<br/>
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,<br/>
ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,<br/>
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,<br/>
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGE Newstate, int bufferlength,<br/>
int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;<br/>
public const int TOKEN_DUPLICATE = 0x00000002;<br/>
public const int TOKEN_IMPERSONATE = 0x00000004;<br/>
public const int TOKEN_QUERY = 0x00000008;<br/>
public const int TOKEN_QUERY_SOURCE = 0x00000010;<br/>
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;<br/>
public const int TOKEN_ADJUST_GROUPS = 0x00000040;<br/>
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;<br/>
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;<br/>
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;<br/>
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)<br/>
{<br/>
try<br/>
{<br/>
int token = 0;<br/>
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();<br/>
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);<br/>
retVal = LookupPrivilegeValue(null, privilege, ref LD);<br/>
TP.PrivilegeCount = 1;
var luidAndAtt = new LUID_AND_ATTRIBUTES();<br/>
luidAndAtt.Attributes = SE_PRIVILEGE_ENABLED;<br/>
luidAndAtt.Luid = LD;<br/>
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
return false;<br/>
}<br/>
}
public static bool DisablePrivilege(string privilege)<br/>
{<br/>
try<br/>
{<br/>
int token = 0;<br/>
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();<br/>
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);<br/>
retVal = LookupPrivilegeValue(null, privilege, ref LD);<br/>
TP.PrivilegeCount = 1;<br/>
// TP.Attributes should be none (not set) to disable privilege
<br/>
var luidAndAtt = new LUID_AND_ATTRIBUTES();<br/>
luidAndAtt.Luid = LD;<br/>
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
return false;<br/>
}<br/>
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct LUID<br/>
{<br/>
internal uint LowPart;<br/>
internal uint HighPart;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct LUID_AND_ATTRIBUTES<br/>
{<br/>
internal LUID Luid;<br/>
internal uint Attributes;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct TOKEN_PRIVILEGE<br/>
{<br/>
internal uint PrivilegeCount;<br/>
internal LUID_AND_ATTRIBUTES Privilege;<br/>
}
public class SetTimeZone<br/>
{<br/>
public const int ERROR_ACCESS_DENIED = 0x005;<br/>
public const int CORSEC_E_MISSING_STRONGNAME = -2146233317;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]<br/>
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
public bool SetSystemTimeZone(string ToTimeZone, out int lastError)<br/>
{<br/>
var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");<br/>
bool IsValidTimeZoneName = false;
// Check if the Time Zone Name is Valid. If valid assign "True" to "CheckValidTimeZoneName"<br/>
foreach (var subKeyname in regTimeZones.GetSubKeyNames())<br/>
{<br/>
if (subKeyname == ToTimeZone)<br/>
{<br/>
IsValidTimeZoneName = true;<br/>
break;<br/>
}<br/>
}
if (IsValidTimeZoneName == true)<br/>
{<br/>
var subKey = regTimeZones.GetSubKeyNames().Where(s => s == ToTimeZone).First();<br/>
string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");<br/>
string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");<br/>
byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");
var regTzi = new RegistryTimeZoneInformation(tzi);
var tz = new TimeZoneInformation();<br/>
tz.Bias = regTzi.Bias;<br/>
tz.DaylightBias = regTzi.DaylightBias;<br/>
tz.StandardBias = regTzi.StandardBias;<br/>
tz.DaylightDate = regTzi.DaylightDate;<br/>
tz.StandardDate = regTzi.StandardDate;<br/>
tz.DaylightName = daylightName;<br/>
tz.StandardName = standardName;
TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");<br/>
bool didSet = SetTimeZone.SetTimeZoneInformation(ref tz);<br/>
lastError = Marshal.GetLastWin32Error();<br/>
TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");
// write the Time Zone Name into "TimeZoneKeyName" Registry
<br/>
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\TimeZoneInformation", true);
key.SetValue("TimeZoneKeyName", key.GetValue("StandardName"));
return didSet;<br/>
}<br/>
else<br/>
{<br/>
//Entered Time Zone is not Present in the System or is not a valid Time Zone. Handle the Exception.<br/>
Console.Write("Entered Time Zone is not Present in the System or is not a valid Time Zone");<br/>
lastError = 0;<br/>
return false;<br/>
}<br/>
}
public static void Main()<br/>
{<br/>
SetTimeZone STZ = new SetTimeZone();<br/>
int lastError;<br/>
bool IsSet = STZ.SetSystemTimeZone("Central Standard Time", out lastError);<br/>
if (IsSet == true)<br/>
{<br/>
Console.WriteLine("Time Zone Set Properly");<br/>
}<br/>
else<br/>
{<br/>
Console.WriteLine("Time Zone setting failed");<br/>
if (lastError == SetTimeZone.ERROR_ACCESS_DENIED)<br/>
{<br/>
Console.WriteLine("Error: Access denied. Try running application as administrator.");<br/>
}<br/>
else if (lastError == SetTimeZone.CORSEC_E_MISSING_STRONGNAME)<br/>
{<br/>
Console.WriteLine("Error: Application is not signed . Right click the project > Signing > Check Sign the assembly.");<br/>
}<br/>
else<br/>
{<br/>
Console.WriteLine("Win32Error: " + lastError + "nHRESULT: " + Marshal.GetHRForLastWin32Error());<br/>
}
<br/>
}<br/>
Console.ReadKey();<br/>
}<br/>
}<br/>
}
=================================================
View the full article
Below is the working C# code for setting system time zone.
This is working perfectly fine on Windows XP and Windows 7.
I need to know if this works on Windows Vista/ Windows Server 2008.
Can any one help me in this if you have Vista in your system?
Thanks,
Vinayak
===================================================
<br/>
using System; <br/>
using System.Collections.Generic; <br/>
using System.Linq; <br/>
using System.Text; <br/>
using System.Runtime.InteropServices; <br/>
using Microsoft.Win32;<br/>
using System.Security.AccessControl;
namespace TimeZoneTest<br/>
{<br/>
[StructLayoutAttribute(LayoutKind.Sequential)]<br/>
public struct SystemTime<br/>
{<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Year;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Month;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short DayOfWeek;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Day;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Hour;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Minute;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Second;<br/>
[MarshalAs(UnmanagedType.U2)]<br/>
public short Milliseconds;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct TimeZoneInformation<br/>
{<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int Bias;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]<br/>
public string StandardName;<br/>
public SystemTime StandardDate;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int StandardBias;<br/>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]<br/>
public string DaylightName;<br/>
public SystemTime DaylightDate;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int DaylightBias;<br/>
}
[StructLayout(LayoutKind.Sequential)]<br/>
public struct RegistryTimeZoneInformation<br/>
{<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int Bias;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int StandardBias;<br/>
[MarshalAs(UnmanagedType.I4)]<br/>
public int DaylightBias;<br/>
public SystemTime StandardDate;<br/>
public SystemTime DaylightDate;
public RegistryTimeZoneInformation(TimeZoneInformation tzi)<br/>
{<br/>
this.Bias = tzi.Bias;<br/>
this.StandardDate = tzi.StandardDate;<br/>
this.StandardBias = tzi.StandardBias;<br/>
this.DaylightDate = tzi.DaylightDate;<br/>
this.DaylightBias = tzi.DaylightBias;<br/>
}
public RegistryTimeZoneInformation(byte[] bytes)<br/>
{<br/>
if ((bytes == null) || (bytes.Length != 0x2c))<br/>
{<br/>
throw new ArgumentException("Argument_InvalidREG_TZI_FORMAT");<br/>
}<br/>
this.Bias = BitConverter.ToInt32(bytes, 0);<br/>
this.StandardBias = BitConverter.ToInt32(bytes, 4);<br/>
this.DaylightBias = BitConverter.ToInt32(bytes, 8);<br/>
this.StandardDate.Year = BitConverter.ToInt16(bytes, 12);<br/>
this.StandardDate.Month = BitConverter.ToInt16(bytes, 14);<br/>
this.StandardDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x10);<br/>
this.StandardDate.Day = BitConverter.ToInt16(bytes, 0x12);<br/>
this.StandardDate.Hour = BitConverter.ToInt16(bytes, 20);<br/>
this.StandardDate.Minute = BitConverter.ToInt16(bytes, 0x16);<br/>
this.StandardDate.Second = BitConverter.ToInt16(bytes, 0x18);<br/>
this.StandardDate.Milliseconds = BitConverter.ToInt16(bytes, 0x1a);<br/>
this.DaylightDate.Year = BitConverter.ToInt16(bytes, 0x1c);<br/>
this.DaylightDate.Month = BitConverter.ToInt16(bytes, 30);<br/>
this.DaylightDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x20);<br/>
this.DaylightDate.Day = BitConverter.ToInt16(bytes, 0x22);<br/>
this.DaylightDate.Hour = BitConverter.ToInt16(bytes, 0x24);<br/>
this.DaylightDate.Minute = BitConverter.ToInt16(bytes, 0x26);<br/>
this.DaylightDate.Second = BitConverter.ToInt16(bytes, 40);<br/>
this.DaylightDate.Milliseconds = BitConverter.ToInt16(bytes, 0x2a);<br/>
}<br/>
}
public class TokenPrivilegesAccess<br/>
{<br/>
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,<br/>
ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,<br/>
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]<br/>
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,<br/>
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGE Newstate, int bufferlength,<br/>
int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;<br/>
public const int TOKEN_DUPLICATE = 0x00000002;<br/>
public const int TOKEN_IMPERSONATE = 0x00000004;<br/>
public const int TOKEN_QUERY = 0x00000008;<br/>
public const int TOKEN_QUERY_SOURCE = 0x00000010;<br/>
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;<br/>
public const int TOKEN_ADJUST_GROUPS = 0x00000040;<br/>
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;<br/>
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;<br/>
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;<br/>
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)<br/>
{<br/>
try<br/>
{<br/>
int token = 0;<br/>
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();<br/>
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);<br/>
retVal = LookupPrivilegeValue(null, privilege, ref LD);<br/>
TP.PrivilegeCount = 1;
var luidAndAtt = new LUID_AND_ATTRIBUTES();<br/>
luidAndAtt.Attributes = SE_PRIVILEGE_ENABLED;<br/>
luidAndAtt.Luid = LD;<br/>
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
return false;<br/>
}<br/>
}
public static bool DisablePrivilege(string privilege)<br/>
{<br/>
try<br/>
{<br/>
int token = 0;<br/>
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();<br/>
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);<br/>
retVal = LookupPrivilegeValue(null, privilege, ref LD);<br/>
TP.PrivilegeCount = 1;<br/>
// TP.Attributes should be none (not set) to disable privilege
<br/>
var luidAndAtt = new LUID_AND_ATTRIBUTES();<br/>
luidAndAtt.Luid = LD;<br/>
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);<br/>
return true;<br/>
}<br/>
catch<br/>
{<br/>
return false;<br/>
}<br/>
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct LUID<br/>
{<br/>
internal uint LowPart;<br/>
internal uint HighPart;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct LUID_AND_ATTRIBUTES<br/>
{<br/>
internal LUID Luid;<br/>
internal uint Attributes;<br/>
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br/>
public struct TOKEN_PRIVILEGE<br/>
{<br/>
internal uint PrivilegeCount;<br/>
internal LUID_AND_ATTRIBUTES Privilege;<br/>
}
public class SetTimeZone<br/>
{<br/>
public const int ERROR_ACCESS_DENIED = 0x005;<br/>
public const int CORSEC_E_MISSING_STRONGNAME = -2146233317;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]<br/>
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
public bool SetSystemTimeZone(string ToTimeZone, out int lastError)<br/>
{<br/>
var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");<br/>
bool IsValidTimeZoneName = false;
// Check if the Time Zone Name is Valid. If valid assign "True" to "CheckValidTimeZoneName"<br/>
foreach (var subKeyname in regTimeZones.GetSubKeyNames())<br/>
{<br/>
if (subKeyname == ToTimeZone)<br/>
{<br/>
IsValidTimeZoneName = true;<br/>
break;<br/>
}<br/>
}
if (IsValidTimeZoneName == true)<br/>
{<br/>
var subKey = regTimeZones.GetSubKeyNames().Where(s => s == ToTimeZone).First();<br/>
string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");<br/>
string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");<br/>
byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");
var regTzi = new RegistryTimeZoneInformation(tzi);
var tz = new TimeZoneInformation();<br/>
tz.Bias = regTzi.Bias;<br/>
tz.DaylightBias = regTzi.DaylightBias;<br/>
tz.StandardBias = regTzi.StandardBias;<br/>
tz.DaylightDate = regTzi.DaylightDate;<br/>
tz.StandardDate = regTzi.StandardDate;<br/>
tz.DaylightName = daylightName;<br/>
tz.StandardName = standardName;
TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");<br/>
bool didSet = SetTimeZone.SetTimeZoneInformation(ref tz);<br/>
lastError = Marshal.GetLastWin32Error();<br/>
TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");
// write the Time Zone Name into "TimeZoneKeyName" Registry
<br/>
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\TimeZoneInformation", true);
key.SetValue("TimeZoneKeyName", key.GetValue("StandardName"));
return didSet;<br/>
}<br/>
else<br/>
{<br/>
//Entered Time Zone is not Present in the System or is not a valid Time Zone. Handle the Exception.<br/>
Console.Write("Entered Time Zone is not Present in the System or is not a valid Time Zone");<br/>
lastError = 0;<br/>
return false;<br/>
}<br/>
}
public static void Main()<br/>
{<br/>
SetTimeZone STZ = new SetTimeZone();<br/>
int lastError;<br/>
bool IsSet = STZ.SetSystemTimeZone("Central Standard Time", out lastError);<br/>
if (IsSet == true)<br/>
{<br/>
Console.WriteLine("Time Zone Set Properly");<br/>
}<br/>
else<br/>
{<br/>
Console.WriteLine("Time Zone setting failed");<br/>
if (lastError == SetTimeZone.ERROR_ACCESS_DENIED)<br/>
{<br/>
Console.WriteLine("Error: Access denied. Try running application as administrator.");<br/>
}<br/>
else if (lastError == SetTimeZone.CORSEC_E_MISSING_STRONGNAME)<br/>
{<br/>
Console.WriteLine("Error: Application is not signed . Right click the project > Signing > Check Sign the assembly.");<br/>
}<br/>
else<br/>
{<br/>
Console.WriteLine("Win32Error: " + lastError + "nHRESULT: " + Marshal.GetHRForLastWin32Error());<br/>
}
<br/>
}<br/>
Console.ReadKey();<br/>
}<br/>
}<br/>
}
=================================================
View the full article