Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
void Main()
{
System.TimeZoneInfo.Local.Dump();
}
/*
// (UTC+09:00) Osaka, Sapporo, Tokyo
Id : Tokyo Standard Time
DisplayName : (UTC+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Summer Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
*/
void Main()
{
System.TimeZoneInfo.GetSystemTimeZones().Dump();
}
Id DisplayName StandardName DaylightName BaseUtcOffset SupportsDaylightSavingTime
Dateline Standard Time (UTC-12:00) International Date Line West Dateline Standard Time Dateline Summer Time -12:00:00 False
UTC-11 (UTC-11:00) Co-ordinated Universal Time-11 UTC-11 UTC-11 -11:00:00 False
Aleutian Standard Time (UTC-10:00) Aleutian Islands Aleutian Standard Time Aleutian Summer Time -10:00:00 True
Hawaiian Standard Time (UTC-10:00) Hawaii Hawaiian Standard Time Hawaiian Summer Time -10:00:00 False
Marquesas Standard Time (UTC-09:30) Marquesas Islands Marquesas Standard Time Marquesas Summer Time -09:30:00 False
Alaskan Standard Time (UTC-09:00) Alaska Alaskan Standard Time Alaskan Summer Time -09:00:00 True
UTC-09 (UTC-09:00) Co-ordinated Universal Time-09 UTC-09 UTC-09 -09:00:00 False
Pacific Standard Time (Mexico) (UTC-08:00) Baja California Pacific Standard Time (Mexico) Pacific Summer Time (Mexico) -08:00:00 True
UTC-08 (UTC-08:00) Co-ordinated Universal Time-08 UTC-08 UTC-08 -08:00:00 False
Pacific Standard Time (UTC-08:00) Pacific Time (US & Canada) Pacific Standard Time Pacific Summer Time -08:00:00 True
US Mountain Standard Time (UTC-07:00) Arizona US Mountain Standard Time US Mountain Summer Time -07:00:00 False
Mountain Standard Time (Mexico) (UTC-07:00) Chihuahua, La Paz, Mazatlan Mountain Standard Time (Mexico) Mountain Summer Time (Mexico) -07:00:00 True
Mountain Standard Time (UTC-07:00) Mountain Time (US & Canada) Mountain Standard Time Mountain Summer Time -07:00:00 True
Central America Standard Time (UTC-06:00) Central America Central America Standard Time Central America Summer Time -06:00:00 False
Central Standard Time (UTC-06:00) Central Time (US & Canada) Central Standard Time Central Summer Time -06:00:00 True
Easter Island Standard Time (UTC-06:00) Easter Island Easter Island Standard Time Easter Island Summer Time -06:00:00 True
Central Standard Time (Mexico) (UTC-06:00) Guadalajara, Mexico City, Monterrey Central Standard Time (Mexico) Central Summer Time (Mexico) -06:00:00 True
Canada Central Standard Time (UTC-06:00) Saskatchewan Canada Central Standard Time Canada Central Summer Time -06:00:00 False
SA Pacific Standard Time (UTC-05:00) Bogota, Lima, Quito, Rio Branco SA Pacific Standard Time SA Pacific Summer Time -05:00:00 False
Eastern Standard Time (Mexico) (UTC-05:00) Chetumal Eastern Standard Time (Mexico) Eastern Summer Time (Mexico) -05:00:00 True
Eastern Standard Time (UTC-05:00) Eastern Time (US & Canada) Eastern Standard Time Eastern Summer Time -05:00:00 True
Haiti Standard Time (UTC-05:00) Haiti Haiti Standard Time Haiti Summer Time -05:00:00 True
Cuba Standard Time (UTC-05:00) Havana Cuba Standard Time Cuba Summer Time -05:00:00 True
US Eastern Standard Time (UTC-05:00) Indiana (East) US Eastern Standard Time US Eastern Summer Time -05:00:00 True
Paraguay Standard Time (UTC-04:00) Asuncion Paraguay Standard Time Paraguay Summer Time -04:00:00 True
Atlantic Standard Time (UTC-04:00) Atlantic Time (Canada) Atlantic Standard Time Atlantic Summer Time -04:00:00 True
Venezuela Standard Time (UTC-04:00) Caracas Venezuela Standard Time Venezuela Summer Time -04:00:00 True
Central Brazilian Standard Time (UTC-04:00) Cuiaba Central Brazilian Standard Time Central Brazilian Summer Time -04:00:00 True
SA Western Standard Time (UTC-04:00) Georgetown, La Paz, Manaus, San Juan SA Western Standard Time SA Western Summer Time -04:00:00 False
Pacific SA Standard Time (UTC-04:00) Santiago Pacific SA Standard Time Pacific SA Summer Time -04:00:00 True
Turks And Caicos Standard Time (UTC-04:00) Turks and Caicos Turks and Caicos Standard Time Turks and Caicos Summer Time -04:00:00 True
Newfoundland Standard Time (UTC-03:30) Newfoundland Newfoundland Standard Time Newfoundland Summer Time -03:30:00 True
Tocantins Standard Time (UTC-03:00) Araguaina Tocantins Standard Time Tocantins Summer Time -03:00:00 True
E. South America Standard Time (UTC-03:00) Brasilia E. South America Standard Time E. South America Summer Time -03:00:00 True
SA Eastern Standard Time (UTC-03:00) Cayenne, Fortaleza SA Eastern Standard Time SA Eastern Summer Time -03:00:00 False
Argentina Standard Time (UTC-03:00) City of Buenos Aires Argentina Standard Time Argentina Summer Time -03:00:00 True
Greenland Standard Time (UTC-03:00) Greenland Greenland Standard Time Greenland Summer Time -03:00:00 True
Montevideo Standard Time (UTC-03:00) Montevideo Montevideo Standard Time Montevideo Summer Time -03:00:00 True
Magallanes Standard Time (UTC-03:00) Punta Arenas Magallanes Standard Time Magallanes Daylight Time -03:00:00 True
Saint Pierre Standard Time (UTC-03:00) Saint Pierre and Miquelon Saint Pierre Standard Time Saint Pierre Summer Time -03:00:00 True
Bahia Standard Time (UTC-03:00) Salvador Bahia Standard Time Bahia Summer Time -03:00:00 True
UTC-02 (UTC-02:00) Co-ordinated Universal Time-02 UTC-02 UTC-02 -02:00:00 False
Mid-Atlantic Standard Time (UTC-02:00) Mid-Atlantic - Old Mid-Atlantic Standard Time Mid-Atlantic Summer Time -02:00:00 True
Azores Standard Time (UTC-01:00) Azores Azores Standard Time Azores SummerTime -01:00:00 True
Cape Verde Standard Time (UTC-01:00) Cabo Verde Is. Cabo Verde Standard Time Cabo Verde Summer Time -01:00:00 False
UTC (UTC) Co-ordinated Universal Time Co-ordinated Universal Time Co-ordinated Universal Time 00:00:00 False
Morocco Standard Time (UTC+00:00) Casablanca Morocco Standard Time Morocco Summer Time 00:00:00 True
GMT Standard Time (UTC+00:00) Dublin, Edinburgh, Lisbon, London GMT Standard Time GMT Summer Time 00:00:00 True
Greenwich Standard Time (UTC+00:00) Monrovia, Reykjavik Greenwich Standard Time Greenwich Summer Time 00:00:00 False
W. Europe Standard Time (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna W. Europe Standard Time W. Europe Summer Time 01:00:00 True
Central Europe Standard Time (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague Central Europe Standard Time Central Europe Summer Time 01:00:00 True
Romance Standard Time (UTC+01:00) Brussels, Copenhagen, Madrid, Paris Romance Standard Time Romance Summer Time 01:00:00 True
Central European Standard Time (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb Central European Standard Time Central European Summer Time 01:00:00 True
W. Central Africa Standard Time (UTC+01:00) West Central Africa W. Central Africa Standard Time W. Central Africa Summer Time 01:00:00 False
Namibia Standard Time (UTC+01:00) Windhoek Namibia Standard Time Namibia Summer Time 01:00:00 True
Jordan Standard Time (UTC+02:00) Amman Jordan Standard Time Jordan Summer Time 02:00:00 True
GTB Standard Time (UTC+02:00) Athens, Bucharest GTB Standard Time GTB Summer Time 02:00:00 True
Middle East Standard Time (UTC+02:00) Beirut Middle East Standard Time Middle East Summer Time 02:00:00 True
Egypt Standard Time (UTC+02:00) Cairo Egypt Standard Time Egypt Summer Time 02:00:00 True
E. Europe Standard Time (UTC+02:00) Chisinau E. Europe Standard Time E. Europe Summer Time 02:00:00 True
Syria Standard Time (UTC+02:00) Damascus Syria Standard Time Syria Summer Time 02:00:00 True
West Bank Standard Time (UTC+02:00) Gaza, Hebron West Bank Gaza Standard Time West Bank Gaza Summer Time 02:00:00 True
South Africa Standard Time (UTC+02:00) Harare, Pretoria South Africa Standard Time South Africa Summer Time 02:00:00 False
FLE Standard Time (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius FLE Standard Time FLE Summer Time 02:00:00 True
Israel Standard Time (UTC+02:00) Jerusalem Jerusalem Standard Time Jerusalem Summer Time 02:00:00 True
Kaliningrad Standard Time (UTC+02:00) Kaliningrad Russia TZ 1 Standard Time Russia TZ 1 Summer Time 02:00:00 True
Libya Standard Time (UTC+02:00) Tripoli Libya Standard Time Libya Summer Time 02:00:00 True
Arabic Standard Time (UTC+03:00) Baghdad Arabic Standard Time Arabic Summer Time 03:00:00 True
Turkey Standard Time (UTC+03:00) Istanbul Turkey Standard Time Turkey Summer Time 03:00:00 True
Arab Standard Time (UTC+03:00) Kuwait, Riyadh Arab Standard Time Arab Summer Time 03:00:00 False
Belarus Standard Time (UTC+03:00) Minsk Belarus Standard Time Belarus Summer Time 03:00:00 True
Russian Standard Time (UTC+03:00) Moscow, St. Petersburg, Volgograd Russia TZ 2 Standard Time Russia TZ 2 Summer Time 03:00:00 True
E. Africa Standard Time (UTC+03:00) Nairobi E. Africa Standard Time E. Africa Summer Time 03:00:00 False
Iran Standard Time (UTC+03:30) Tehran Iran Standard Time Iran Summer Time 03:30:00 True
Arabian Standard Time (UTC+04:00) Abu Dhabi, Muscat Arabian Standard Time Arabian Summer Time 04:00:00 False
Astrakhan Standard Time (UTC+04:00) Astrakhan, Ulyanovsk Astrakhan Standard Time Astrakhan Summer Time 04:00:00 True
Azerbaijan Standard Time (UTC+04:00) Baku Azerbaijan Standard Time Azerbaijan Summer Time 04:00:00 True
Russia Time Zone 3 (UTC+04:00) Izhevsk, Samara Russia TZ 3 Standard Time Russia TZ 3 Summer Time 04:00:00 True
Mauritius Standard Time (UTC+04:00) Port Louis Mauritius Standard Time Mauritius Summer Time 04:00:00 True
Saratov Standard Time (UTC+04:00) Saratov Saratov Standard Time Saratov Summer Time 04:00:00 True
Georgian Standard Time (UTC+04:00) Tbilisi Georgian Standard Time Georgian Summer Time 04:00:00 False
Caucasus Standard Time (UTC+04:00) Yerevan Caucasus Standard Time Caucasus Summer Time 04:00:00 True
Afghanistan Standard Time (UTC+04:30) Kabul Afghanistan Standard Time Afghanistan Summer Time 04:30:00 False
West Asia Standard Time (UTC+05:00) Ashgabat, Tashkent West Asia Standard Time West Asia Summer Time 05:00:00 False
Ekaterinburg Standard Time (UTC+05:00) Ekaterinburg Russia TZ 4 Standard Time Russia TZ 4 Summer Time 05:00:00 True
Pakistan Standard Time (UTC+05:00) Islamabad, Karachi Pakistan Standard Time Pakistan Summer Time 05:00:00 True
India Standard Time (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time India Summer Time 05:30:00 False
Sri Lanka Standard Time (UTC+05:30) Sri Jayawardenepura Sri Lanka Standard Time Sri Lanka Summer Time 05:30:00 False
Nepal Standard Time (UTC+05:45) Kathmandu Nepal Standard Time Nepal Summer Time 05:45:00 False
Central Asia Standard Time (UTC+06:00) Astana Central Asia Standard Time Central Asia Summer Time 06:00:00 False
Bangladesh Standard Time (UTC+06:00) Dhaka Bangladesh Standard Time Bangladesh Summer Time 06:00:00 True
Omsk Standard Time (UTC+06:00) Omsk Omsk Standard Time Omsk Summer Time 06:00:00 True
Myanmar Standard Time (UTC+06:30) Yangon (Rangoon) Myanmar Standard Time Myanmar Summer Time 06:30:00 False
SE Asia Standard Time (UTC+07:00) Bangkok, Hanoi, Jakarta SE Asia Standard Time SE Asia Summer Time 07:00:00 False
Altai Standard Time (UTC+07:00) Barnaul, Gorno-Altaysk Altai Standard Time Altai Summer Time 07:00:00 True
W. Mongolia Standard Time (UTC+07:00) Hovd W. Mongolia Standard Time W. Mongolia Summer Time 07:00:00 True
North Asia Standard Time (UTC+07:00) Krasnoyarsk Russia TZ 6 Standard Time Russia TZ 6 Summer Time 07:00:00 True
N. Central Asia Standard Time (UTC+07:00) Novosibirsk Novosibirsk Standard Time Novosibirsk Summer Time 07:00:00 True
Tomsk Standard Time (UTC+07:00) Tomsk Tomsk Standard Time Tomsk Summer Time 07:00:00 True
China Standard Time (UTC+08:00) Beijing, Chongqing, Hong Kong SAR, Urumqi China Standard Time China Summer Time 08:00:00 False
North Asia East Standard Time (UTC+08:00) Irkutsk Russia TZ 7 Standard Time Russia TZ 7 Summer Time 08:00:00 True
Singapore Standard Time (UTC+08:00) Kuala Lumpur, Singapore Malay Peninsula Standard Time Malay Peninsula Summer Time 08:00:00 False
W. Australia Standard Time (UTC+08:00) Perth W. Australia Standard Time W. Australia Summer Time 08:00:00 True
Taipei Standard Time (UTC+08:00) Taipei Taipei Standard Time Taipei Summer Time 08:00:00 False
Ulaanbaatar Standard Time (UTC+08:00) Ulaanbaatar Ulaanbaatar Standard Time Ulaanbaatar Summer Time 08:00:00 True
North Korea Standard Time (UTC+08:30) Pyongyang North Korea Standard Time North Korea Summer Time 08:30:00 True
Aus Central W. Standard Time (UTC+08:45) Eucla Aus Central W. Standard Time Aus Central W. Summer Time 08:45:00 False
Transbaikal Standard Time (UTC+09:00) Chita Transbaikal Standard Time Transbaikal Summer Time 09:00:00 True
Tokyo Standard Time (UTC+09:00) Osaka, Sapporo, Tokyo Tokyo Standard Time Tokyo Summer Time 09:00:00 False
Korea Standard Time (UTC+09:00) Seoul Korea Standard Time Korea Summer Time 09:00:00 False
Yakutsk Standard Time (UTC+09:00) Yakutsk Russia TZ 8 Standard Time Russia TZ 8 Summer Time 09:00:00 True
Cen. Australia Standard Time (UTC+09:30) Adelaide Cen. Australia Standard Time Cen. Australia Summer Time 09:30:00 True
AUS Central Standard Time (UTC+09:30) Darwin AUS Central Standard Time AUS Central Summer Time 09:30:00 False
E. Australia Standard Time (UTC+10:00) Brisbane E. Australia Standard Time E. Australia Summer Time 10:00:00 False
AUS Eastern Standard Time (UTC+10:00) Canberra, Melbourne, Sydney AUS Eastern Standard Time AUS Eastern Summer Time 10:00:00 True
West Pacific Standard Time (UTC+10:00) Guam, Port Moresby West Pacific Standard Time West Pacific Summer Time 10:00:00 False
Tasmania Standard Time (UTC+10:00) Hobart Tasmania Standard Time Tasmania Summer Time 10:00:00 True
Vladivostok Standard Time (UTC+10:00) Vladivostok Russia TZ 9 Standard Time Russia TZ 9 Summer Time 10:00:00 True
Lord Howe Standard Time (UTC+10:30) Lord Howe Island Lord Howe Standard Time Lord Howe Summer Time 10:30:00 True
Bougainville Standard Time (UTC+11:00) Bougainville Island Bougainville Standard Time Bougainville Summer Time 11:00:00 True
Russia Time Zone 10 (UTC+11:00) Chokurdakh Russia TZ 10 Standard Time Russia TZ 10 Summer Time 11:00:00 True
Magadan Standard Time (UTC+11:00) Magadan Magadan Standard Time Magadan Summer Time 11:00:00 True
Norfolk Standard Time (UTC+11:00) Norfolk Island Norfolk Standard Time Norfolk Summer Time 11:00:00 True
Sakhalin Standard Time (UTC+11:00) Sakhalin Sakhalin Standard Time Sakhalin Summer Time 11:00:00 True
Central Pacific Standard Time (UTC+11:00) Solomon Is., New Caledonia Central Pacific Standard Time Central Pacific Summer Time 11:00:00 False
Russia Time Zone 11 (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky Russia TZ 11 Standard Time Russia TZ 11 Summer Time 12:00:00 True
New Zealand Standard Time (UTC+12:00) Auckland, Wellington New Zealand Standard Time New Zealand Summer Time 12:00:00 True
UTC+12 (UTC+12:00) Co-ordinated Universal Time+12 UTC+12 UTC+12 12:00:00 False
Fiji Standard Time (UTC+12:00) Fiji Fiji Standard Time Fiji Summer Time 12:00:00 True
Kamchatka Standard Time (UTC+12:00) Petropavlovsk-Kamchatsky - Old Kamchatka Standard Time Kamchatka Summer Time 12:00:00 True
Chatham Islands Standard Time (UTC+12:45) Chatham Islands Chatham Islands Standard Time Chatham Islands Summer Time 12:45:00 True
UTC+13 (UTC+13:00) Co-ordinated Universal Time+13 UTC+13 UTC+13 13:00:00 False
Tonga Standard Time (UTC+13:00) Nuku'alofa Tonga Standard Time Tonga Summer Time 13:00:00 True
Samoa Standard Time (UTC+13:00) Samoa Samoa Standard Time Samoa Summer Time 13:00:00 True
Line Islands Standard Time (UTC+14:00) Kiritimati Island Line Islands Standard Time Line Islands Summer Time 14:00:00 False
using Microsoft.Win32;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
TimeZone.Set("Line Islands Standard Time");
TimeZone.Set("Tokyo Standard Time");
}
}
public class TimeZone
{
public const int ERROR_ACCESS_DENIED = 0x005;
public const int CORSEC_E_MISSING_STRONGNAME = -2146233317;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);
public static void Set(string name)
{
using (var regTimeZones = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"))
{
var subKey = regTimeZones.GetSubKeyNames().Where(s => s == name).First();
var daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");
var standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");
var tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");
var regTzi = new RegistryTimeZoneInformation(tzi);
TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");
bool didSet;
if (Environment.OSVersion.Version.Major < 6)
{
var tz = new TimeZoneInformation()
{
Bias = regTzi.Bias,
DaylightBias = regTzi.DaylightBias,
StandardBias = regTzi.StandardBias,
DaylightDate = regTzi.DaylightDate,
StandardDate = regTzi.StandardDate,
DaylightName = daylightName,
StandardName = standardName
};
didSet = SetTimeZoneInformation(ref tz);
}
else
{
var tz = new DynamicTimeZoneInformation()
{
Bias = regTzi.Bias,
DaylightBias = regTzi.DaylightBias,
StandardBias = regTzi.StandardBias,
DaylightDate = regTzi.DaylightDate,
StandardDate = regTzi.StandardDate,
DaylightName = daylightName,
StandardName = standardName,
TimeZoneKeyName = subKey,
DynamicDaylightTimeDisabled = false
};
didSet = SetDynamicTimeZoneInformation(ref tz);
}
var lastError = Marshal.GetLastWin32Error();
TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");
if (!didSet)
{
if (lastError == ERROR_ACCESS_DENIED)
{
throw new SecurityException("Access denied changing System Timezone.");
}
else if (lastError == CORSEC_E_MISSING_STRONGNAME)
{
throw new SystemException("Application is not signed.");
}
else
{
throw new SystemException("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error());
}
}
}
}
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string StandardName;
public SystemTime StandardDate;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string DaylightName;
public SystemTime DaylightDate;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DynamicTimeZoneInformation
{
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;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string TimeZoneKeyName;
[MarshalAs(UnmanagedType.U1)]
public bool DynamicDaylightTimeDisabled;
}
[StructLayout(LayoutKind.Sequential)]
public struct RegistryTimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
public SystemTime StandardDate;
public SystemTime DaylightDate;
public RegistryTimeZoneInformation(TimeZoneInformation tzi)
{
Bias = tzi.Bias;
StandardDate = tzi.StandardDate;
StandardBias = tzi.StandardBias;
DaylightDate = tzi.DaylightDate;
DaylightBias = tzi.DaylightBias;
}
public RegistryTimeZoneInformation(byte[] bytes)
{
if ((bytes == null) || (bytes.Length != 0x2c))
{
throw new ArgumentException("Argument_InvalidREG_TZI_FORMAT");
}
Bias = BitConverter.ToInt32(bytes, 0);
StandardBias = BitConverter.ToInt32(bytes, 4);
DaylightBias = BitConverter.ToInt32(bytes, 8);
StandardDate.Year = BitConverter.ToInt16(bytes, 12);
StandardDate.Month = BitConverter.ToInt16(bytes, 14);
StandardDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x10);
StandardDate.Day = BitConverter.ToInt16(bytes, 0x12);
StandardDate.Hour = BitConverter.ToInt16(bytes, 20);
StandardDate.Minute = BitConverter.ToInt16(bytes, 0x16);
StandardDate.Second = BitConverter.ToInt16(bytes, 0x18);
StandardDate.Milliseconds = BitConverter.ToInt16(bytes, 0x1a);
DaylightDate.Year = BitConverter.ToInt16(bytes, 0x1c);
DaylightDate.Month = BitConverter.ToInt16(bytes, 30);
DaylightDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x20);
DaylightDate.Day = BitConverter.ToInt16(bytes, 0x22);
DaylightDate.Hour = BitConverter.ToInt16(bytes, 0x24);
DaylightDate.Minute = BitConverter.ToInt16(bytes, 0x26);
DaylightDate.Second = BitConverter.ToInt16(bytes, 40);
DaylightDate.Milliseconds = BitConverter.ToInt16(bytes, 0x2a);
}
}
public class TokenPrivilegesAccess
{
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGE Newstate, int bufferlength,
int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;
public const int TOKEN_DUPLICATE = 0x00000002;
public const int TOKEN_IMPERSONATE = 0x00000004;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_QUERY_SOURCE = 0x00000010;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_ADJUST_GROUPS = 0x00000040;
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)
{
try
{
var token = 0;
var retVal = 0;
var TP = new TOKEN_PRIVILEGE();
var LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
var luidAndAtt = new LUID_AND_ATTRIBUTES();
luidAndAtt.Attributes = SE_PRIVILEGE_ENABLED;
luidAndAtt.Luid = LD;
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
public static bool DisablePrivilege(string privilege)
{
try
{
var token = 0;
var retVal = 0;
var TP = new TOKEN_PRIVILEGE();
var LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
// TP.Attributes should be none (not set) to disable privilege
var luidAndAtt = new LUID_AND_ATTRIBUTES()
{
Luid = LD
};
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID
{
internal uint LowPart;
internal uint HighPart;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID_AND_ATTRIBUTES
{
internal LUID Luid;
internal uint Attributes;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TOKEN_PRIVILEGE
{
internal uint PrivilegeCount;
internal LUID_AND_ATTRIBUTES Privilege;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment