Muokkaa

Jaa


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 Ildasm.exe (IL Disassembler) 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.

Example

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", this.GetType().Assembly);

   // 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;
   }
}
Private Sub DeserializeTimeZones()
    Dim cst, palmer As TimeZoneInfo
    Dim timeZoneString As String
    Dim resMgr As ResourceManager = New ResourceManager("SerializeTimeZoneData.SerializedTimeZones",
                                    GetType(SerializeTimeZoneData).Assembly)

    ' Attempt to retrieve time zone from system
    Try
        cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
    Catch ex As TimeZoneNotFoundException
        ' Time zone not in system; retrieve from resource
        timeZoneString = resMgr.GetString("CentralStandardTime")
        If Not String.IsNullOrEmpty(timeZoneString) Then
            cst = TimeZoneInfo.FromSerializedString(timeZoneString)
        Else
            MsgBox("Unable to create Central Standard Time Zone. Application must exit.")
            Exit Sub
        End If
    End Try
    ' Retrieve custom time zone
    Try
        timeZoneString = resMgr.GetString("PalmerStandardTime")
        palmer = TimeZoneInfo.FromSerializedString(timeZoneString)
    Catch ex As Exception
        MsgBox(ex.GetType().Name & ": Unable to create Palmer Standard Time Zone. Application must exit.")
        Exit Sub
    End Try
End Sub

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.

Compiling the code

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;
    
    Imports System.Globalization
    Imports System.IO
    Imports System.Reflection
    Imports System.Resources
    

See also