C# 中获取时区列表

  • c#中获取时区列表 下面方法获得的仅仅用来显示和使用,无法用来进行时间转换。
 1      
 2 public static List GetSystemTimeZones()
 3         {
 4             List list = new List();
 5 
 6             PermissionSet set = new PermissionSet(PermissionState.None);
 7             set.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"));
 8             set.Assert();
 9             using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones", RegistryKeyPermissionCheck.Default, RegistryRights.ExecuteKey))
10             {
11                 if (key != null)
12                 {
13                     foreach (string str in key.GetSubKeyNames())
14                     {
15                         DisplayTimeZone timeZone = null;
16                         Exception e = null;
17                         TryGetTimeZone(str, out timeZone, out e);
18                         if (timeZone != null)
19                         {
20                             list.Add(timeZone);
21                         }
22                     }
23                 }
24             }
25             return list;
26         }
27 
28         private static void TryGetTimeZone(string id, out DisplayTimeZone value, out Exception e)
29         {
30             e = null;
31             value = new DisplayTimeZone();
32             try
33             {
34                 using (RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", new object[] { @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones", id }), RegistryKeyPermissionCheck.Default, RegistryRights.ExecuteKey))
35                 {
36                     value.DisplayName = key.GetValue("Display", string.Empty, RegistryValueOptions.None) as string;
37                     value.StandardName = key.GetValue("Std", string.Empty, RegistryValueOptions.None) as string;
38                     value.DaylightName = key.GetValue("Dlt", string.Empty, RegistryValueOptions.None) as string;
39                 }
40             }
41             catch (Exception ex)
42             {
43                 e = ex;
44                 value = null;
45             }
46         }
47 
48         public class DisplayTimeZone
49         {
50             public string StandardName;
51             public string DisplayName;
52             public string DaylightName;
53         }

 

转载于:https://www.cnblogs.com/anthonyBlog/p/3628013.html

你可能感兴趣的:(C# 中获取时区列表)