使用C#获取当前Windows所设定的时区

来源:互联网 发布:pop3协议默认端口号 编辑:程序博客网 时间:2024/06/05 16:57
public static TimeZoneInformation[] EnumZones()
         {
           if ( s_zones == null )
         {
                lock( s_lockZones )
                {
                    if ( s_zones == null )
                   {
                         ArrayList zones = new ArrayList();

                      using ( RegistryKey key = 
                                    Registry.LocalMachine.OpenSubKey( 
                                    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
                        {
                            string[] zoneNames = key.GetSubKeyNames();

                            foreach ( string zoneName in zoneNames )

                         {                    

                    using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )


                              {               

                          TimeZoneInformation tzi = new TimeZoneInformation();

                                   tzi.m_name = zoneName;

                                    tzi.m_displayName = (string) subKey.GetValue( "Display" );                       

            tzi.m_standardName = (string) subKey.GetValue( "Std" );



                                   tzi.m_daylightName = (string) subKey.GetValue( "Dlt" );
                                  tzi.m_index = (int)( subKey.GetValue( "Index" ) );

                                   tzi.InitTzi( (byte[]) subKey.GetValue( "Tzi" ) );
                         
                                    zones.Add( tzi );
                                }

                            }

                        }


                        s_zones = new TimeZoneInformation[ zones.Count ];

                      zones.CopyTo( s_zones );
                  }

              }

           }


           return s_zones;
       }
0 0
原创粉丝点击