zoukankan      html  css  js  c++  java
  • C# 中获取时区列表

    • c#中获取时区列表 下面方法获得的仅仅用来显示和使用,无法用来进行时间转换。
     1      
     2 public static List<DisplayTimeZone> GetSystemTimeZones()
     3         {
     4             List<DisplayTimeZone> list = new List<DisplayTimeZone>();
     5 
     6             PermissionSet set = new PermissionSet(PermissionState.None);
     7             set.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionTime Zones"));
     8             set.Assert();
     9             using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindows NTCurrentVersionTime 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[] { @"SOFTWAREMicrosoftWindows NTCurrentVersionTime 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         }
  • 相关阅读:
    RabbitMQ安装
    windows下安装Erlang
    利用StopWatch类监控Java代码执行时间并分析性能
    HttpClinet工具类
    Javap与JVM指令
    idea全局护眼色绿豆沙
    删除注册在Eureka的服务(无效,多余)
    Steam之两个list间交集、并集、差集
    微服务与SpringCloud简介
    VS code 豆沙绿护眼主题
  • 原文地址:https://www.cnblogs.com/anthonyBlog/p/3628013.html
Copyright © 2011-2022 走看看