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 )
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;
}
{
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_displayName = (string) subKey.GetValue( "Display" );
tzi.m_standardName = (string) subKey.GetValue( "Std" );
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;
}
枚举时区信息
本文介绍了一种通过读取注册表来枚举系统中所有可用时区的方法。该方法使用了锁来确保线程安全,并利用.NET Framework提供的RegistryKey类从注册表中获取时区名称及其详细信息。
857

被折叠的 条评论
为什么被折叠?



