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         }
  • 相关阅读:
    Js事件触发列表与解说(转)
    HTTP 头部解释,HTTP 头部详细分析,最全HTTP头部信息
    【转】php 数组 编码转换
    Cookie 的规范介绍
    PHP底层的运行机制与原理
    PHP为什么会被认为是草根语言?
    asp.net中读取带有加号(+)的Cookie,会自动把加号替换为空格
    git克隆某个分支到本地指定的目录中
    yq(json,yaml)格式转换工具安装和使用
    k8s中configmap的作用及使用方式
  • 原文地址:https://www.cnblogs.com/anthonyBlog/p/3628013.html
Copyright © 2011-2022 走看看