Export (0) Print
Expand All

How to: Restore Time Zones from an Embedded Resource

This topic describes how to restore time zones that have been saved in a resource file. For information and instructions about saving time zones, see How to: Save Time Zones to an Embedded Resource.

To deserialize a TimeZoneInfo object from an embedded resource

  1. If the time zone to be retrieved is not a custom time zone, try to instantiate it by using the FindSystemTimeZoneById method.

  2. Instantiate a ResourceManager object by passing the fully qualified name of the embedded resource file and a reference to the assembly that contains the resource file.

    If you cannot determine the fully qualified name of the embedded resource file, use the MSIL Disassembler (Ildasm.exe) to examine the assembly's manifest. An .mresource entry identifies the resource. In the example, the resource's fully qualified name is SerializeTimeZoneData.SerializedTimeZones.

    If the resource file is embedded in the same assembly that contains the time zone instantiation code, you can retrieve a reference to it by calling the static (Shared in Visual Basic) GetExecutingAssembly method.

  3. If the call to the FindSystemTimeZoneById method fails, or if a custom time zone is to be instantiated, retrieve a string that contains the serialized time zone by calling the ResourceManager.GetString method.

  4. Deserialize the time zone data by calling the FromSerializedString method.

The following example deserializes a TimeZoneInfo object stored in an embedded .NET XML resource file.

private void DeserializeTimeZones()
{
   TimeZoneInfo cst, palmer;
   string timeZoneString;
   ResourceManager resMgr = new ResourceManager("SerializeTimeZoneData.SerializedTimeZones", Assembly.GetExecutingAssembly());

   // Attempt to retrieve time zone from system 
   try
   {
      cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   }
   catch (TimeZoneNotFoundException)
   {
      // Time zone not in system; retrieve from resource
      timeZoneString = resMgr.GetString("CentralStandardTime");
      if (! String.IsNullOrEmpty(timeZoneString))
      {
         cst = TimeZoneInfo.FromSerializedString(timeZoneString);
      }
      else
      {
         MessageBox.Show("Unable to create Central Standard Time Zone. Application must exit.", "Application Error");
         return;
      }
   }
   // Retrieve custom time zone 
   try
   {
      timeZoneString = resMgr.GetString("PalmerStandardTime");
      palmer = TimeZoneInfo.FromSerializedString(timeZoneString);
   }
   catch (MissingManifestResourceException) 
   {
      MessageBox.Show("Unable to retrieve the Palmer Standard Time Zone from the resource file. Application must exit.");
      return;
   }
}

This code illustrates exception handling to ensure that a TimeZoneInfo object required by the application is present. It first tries to instantiate a TimeZoneInfo object by retrieving it from the registry using the FindSystemTimeZoneById method. If the time zone cannot be instantiated, the code retrieves it from the embedded resource file.

Because data for custom time zones (time zones instantiated by using the CreateCustomTimeZone method) are not stored in the registry, the code does not call the FindSystemTimeZoneById to instantiate the time zone for Palmer, Antarctica. Instead, it immediately looks to the embedded resource file to retrieve a string that contains the time zone's data before it calls the FromSerializedString method.

This example requires:

  • That a reference to System.Windows.Forms.dll and System.Core.dll be added to the project.

  • That the following namespaces be imported:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Windows.Forms;
    

Community Additions

ADD
Show:
© 2014 Microsoft