作法:從內嵌資源還原時區

本主題描述如何還原已儲存在資源檔中的時區。 如需節省時區的資訊和指示,請參閱如何:將時區儲存到內嵌資源

從內嵌資源還原序列化 TimeZoneInfo 物件

  1. 如果要擷取的時區不是自訂時區,請嘗試使用 FindSystemTimeZoneById 方法來將其具現化。

  2. 您可以傳遞內嵌資源檔的完整名稱,以及包含資源檔的組件參考,藉此具現化 ResourceManager 物件。

    如果您無法判斷內嵌資源檔的完整名稱,請使用 Ildasm.exe (IL 反組譯工具) 來檢查組件的資訊清單。 .mresource 項目可識別資源。 在此範例中,資源的完整名稱為 SerializeTimeZoneData.SerializedTimeZones

    如果資源檔內嵌在包含時區具現化程式碼的相同組件中,您可以呼叫 static (在 Visual Basic 中為 Shared) GetExecutingAssembly 方法來擷取其參考。

  3. 如果呼叫 FindSystemTimeZoneById 方法失敗,或要具現化自訂時區,請呼叫 ResourceManager.GetString 方法來擷取包含序列化時區的字串。

  4. 呼叫 FromSerializedString 方法來還原序列化時區資料。

範例

下列範例會將儲存在內嵌 .NET XML 資源檔中的 TimeZoneInfo 物件還原序列化。

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

此程式碼說明例外狀況處理,以確保應用程式所需的 TimeZoneInfo 物件存在。 其會先嘗試使用 FindSystemTimeZoneById 方法從登錄擷取物件,藉此具現化 TimeZoneInfo 物件。 如果無法具現化時區,則程式碼會從內嵌資源檔加以擷取。

由於自訂時區的資料 (使用 CreateCustomTimeZone 方法具現化的時區) 不會儲存在登錄中,因此程式碼不會呼叫 FindSystemTimeZoneById 來具現化南極大陸帕麥的時區。 相反地,其會立即尋找內嵌資源檔,以擷取包含時區資料的字串,然後再呼叫 FromSerializedString 方法。

編譯程式碼

這個範例需要:

  • 將 System.Windows.Forms.dll 和 System.Core.dll 的參考新增至專案。

  • 匯入下列命名空間:

    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
    

另請參閱