方法 : 埋め込みリソースからタイム ゾーンを復元する

このトピックでは、リソース ファイルに保存されているタイム ゾーンを復元する方法について説明します。 タイム ゾーンの保存に関する詳細と方法については、「方法 : 埋め込みリソースにタイム ゾーンを保存する」を参照してください。

埋め込みリソースから TimeZoneInfo オブジェクトを逆シリアル化するには

  1. 取得するタイム ゾーンがカスタム タイム ゾーンではない場合は、FindSystemTimeZoneById メソッドを使用してインスタンス化を試みます。

  2. 埋め込みリソース ファイルの完全修飾名およびリソース ファイルを含むアセンブリへの参照を渡して、ResourceManager オブジェクトをインスタンス化します。

    埋め込みリソース ファイルの完全修飾名がわからない場合は、Ildasm.exe (MSIL 逆アセンブラー) を使用してアセンブリのマニフェストを調べます。 .mresource エントリがリソースを示しています。 この例では、リソースの完全修飾名は SerializeTimeZoneData.SerializedTimeZones です。

    リソース ファイルが、タイム ゾーンのインスタンス化コードと同じアセンブリに埋め込まれている場合は、static (Visual Basic では Shared) GetExecutingAssembly メソッドを呼び出すことで、リソース ファイルへの参照を取得できます。

  3. FindSystemTimeZoneById メソッドの呼び出しが失敗する場合、またはカスタム タイム ゾーンをインスタンス化する場合は、ResourceManager.GetString メソッドを呼び出して、シリアル化されたタイム ゾーンを含む文字列を取得します。

  4. FromSerializedString メソッドを呼び出して、タイム ゾーン データを逆シリアル化します。

使用例

次の例では、埋め込み .NET XML リソース ファイルに格納されている TimeZoneInfo オブジェクトを逆シリアル化します。

Private Sub DeserializeTimeZones()
   Dim cst, palmer As TimeZoneInfo
   Dim timeZoneString As String
   Dim resMgr As ResourceManager = New ResourceManager("SerializeTimeZoneData.SerializedTimeZones", Assembly.GetExecutingAssembly)

   ' 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
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;
   }
}

このコードは、アプリケーションで必要な TimeZoneInfo オブジェクトが存在することを確認するための例外処理を示しています。 最初に、FindSystemTimeZoneById メソッドを使用してレジストリから取得する方法で、TimeZoneInfo オブジェクトのインスタンス化を試みます。 タイム ゾーンをインスタンス化できない場合は、埋め込みリソース ファイルから取得します。

カスタム タイム ゾーン (CreateCustomTimeZone メソッドを使用してインスタンス化されるタイム ゾーン) のデータはレジストリに格納されていないので、Palmer, Antarctica のタイム ゾーンをインスタンス化するときは FindSystemTimeZoneById を呼び出しません。 代わりに、FromSerializedString メソッドを呼び出す前に、埋め込みリソース ファイルを直接調べてタイム ゾーンのデータを含む文字列を取得します。

コードのコンパイル

この例で必要な要素は次のとおりです。

  • System.Windows.Forms.dll および System.Core.dll への参照をプロジェクトに追加する。

  • 次の名前空間をインポートする。

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

参照

処理手順

方法 : 埋め込みリソースにタイム ゾーンを保存する

概念

タイム ゾーンの概要

その他の技術情報

日付、時刻、およびタイム ゾーン