共用方式為


HOW TO:從內嵌資源還原時區

更新:2007 年 11 月

本主題說明如何將儲存在資源檔案中的時區還原。如需儲存時區的詳細資訊和指示,請參閱 HOW TO:將時區儲存到內嵌資源

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

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

  2. 將內嵌資源檔的完整名稱及參考傳送給內含資源檔的組件,將 ResourceManager 物件具現化。

    如果您無法判斷內嵌資源檔的完整名稱,請使用 MSIL 反組譯工具 (Ildasm.exe) 查看組件的資訊清單。.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 方法具現化的時區) 並沒有儲存在登錄中,所以程式碼並沒有呼叫 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;
    

請參閱

工作

HOW TO:將時區儲存到內嵌資源

概念

時區概觀

其他資源

時間和時區