다음을 통해 공유


방법: 포함 리소스에서 표준 시간대 복원

업데이트: 2007년 11월

이 항목에서는 리소스 파일에서 저장된 표준 시간대를 복원하는 방법에 대해 설명합니다. 표준 시간대를 저장하는 방법에 대한 자세한 내용과 지침은 방법: 포함 리소스에 표준 시간대 저장을 참조하십시오.

포함 리소스에서 TimeZoneInfo 개체를 deserialize하려면

  1. 검색할 표준 시간대가 사용자 지정 표준 시간대가 아닌 경우 FindSystemTimeZoneById 메서드를 사용하여 해당 표준 시간대를 인스턴스화합니다.

  2. 포함 리소스 파일의 정규화된 이름과 리소스 파일을 포함하는 어셈블리에 대한 참조를 전달하여 ResourceManager 개체를 인스턴스화합니다.

    포함 리소스 파일의 정규화된 이름을 확인할 수 없으면 MSIL 디스어셈블러(Ildasm.exe)를 사용하여 어셈블리 매니페스트를 검사합니다. .mresource 엔트리는 리소스를 식별합니다. 이 예제에서 리소스의 정규화된 이름은 SerializeTimeZoneData.SerializedTimeZones입니다.

    리소스 파일이 표준 시간대 인스턴스화 코드가 있는 동일한 어셈블리에 포함되어 있는 경우 static(Visual Basic의 경우 Shared) GetExecutingAssembly 메서드를 호출하여 어셈블리에 대한 참조를 검색할 수 있습니다.

  3. FindSystemTimeZoneById 메서드 호출에 실패하거나 사용자 지정 표준 시간대를 인스턴스화해야 하는 경우 ResourceManager.GetString 메서드를 호출하여 serialize된 표준 시간대가 포함된 문자열을 검색합니다.

  4. FromSerializedString 메서드를 호출하여 표준 시간대 데이터를 deserialize합니다.

예제

다음 예제에서는 포함 .NET XML 리소스 파일에 저장된 TimeZoneInfo 개체를 deserialize합니다.

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;
    

참고 항목

작업

방법: 포함 리소스에 표준 시간대 저장

개념

표준 시간대 개요

기타 리소스

시간 및 표준 시간대