using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.Win32;
namespace VB.TimeZoneInformation
{
/// <summary>
/// Date: 02/18/2008
/// Created By: Vladimir Burmistrovich
/// Exports all of the information about time zones from the Windows registry to an XML file
/// </summary>
public class TimeZoneExporter
{
private List<TimeZoneInformationStruct> timezones; // a list of all the timezones in the
// registry
private const string fileName = "timezones.xml"; // the filename to export to
/// <summary>
/// The entry point of the program
/// </summary>
/// <param name="args">The standard args parameter. It's not used in this program.</param>
static void Main(string[] args)
{
TimeZoneExporter exporter = new TimeZoneExporter();
exporter.SetTimeZoneInfo();
exporter.Export();
}
/// <summary>
/// Exports all of the time zone information to the XML file. Uses the new LINQ
/// functionality of .NET 3.5
/// </summary>
private void Export()
{
XDocument xmlDoc = new XDocument();
XElement timezonesEl = new XElement("TimeZones");
foreach (TimeZoneInformationStruct tz in timezones)
{
XElement timezone = new XElement("TimeZone");
XElement display = new XElement("Display", tz.Display);
XElement dlt = new XElement("Dlt", tz.Dlt);
XElement std = new XElement("Std", tz.Std);
XElement tzi = new XElement("TziBytes");
foreach (byte b in tz.Tzi) // Since Tzi is a byte array, export each byte
// separately for easier importing later
{
XElement tzibyte = new XElement("TziByte", b);
tzi.Add(tzibyte);
}
timezone.Add(display);
timezone.Add(dlt);
timezone.Add(std);
timezone.Add(tzi);
timezonesEl.Add(timezone);
}
xmlDoc.Add(timezonesEl);
xmlDoc.Save(fileName);
}
/// <summary>
/// Loads the timezones list with the information from the Windows Registry.
/// </summary>
private void SetTimeZoneInfo()
{
timezones = new List<TimeZoneInformationStruct>();
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))
{
TimeZoneInformationStruct timezone = new TimeZoneInformationStruct();
timezone.Display = (string)subKey.GetValue("Display");
timezone.Std = (string)subKey.GetValue("Std");
timezone.Dlt = (string)subKey.GetValue("Dlt");
timezone.Tzi = (byte[])subKey.GetValue("Tzi");
timezones.Add(timezone);
}
}
}
}
}
/// <summary>
/// A struct with all the needed info contained in the registry.
/// </summary>
struct TimeZoneInformationStruct
{
public string Display
{ get; set; }
public string Std
{ get; set; }
public string Dlt
{ get; set; }
public byte[] Tzi
{ get; set; }
}
}