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         }
  • 相关阅读:
    异步请求 ajax的使用详解
    关于JAVA-JS-JSP之间传值的各种方法
    C# 对List<T>取交集、连集及差集
    MVC解决方案发布IIS 登录页面需要输入两次帐号问题
    sqlserver查看被锁表、解锁
    C#中using关键字的作用及用法
    Bootstrap组件On和Off语法
    HtmlString类创建HTML Hepler 扩展MVC TextBox组件
    Js运算符优先级
    ajax调用webservice(二) 跨域。
  • 原文地址:https://www.cnblogs.com/anthonyBlog/p/3628013.html
Copyright © 2011-2022 走看看