CultureAndRegionInfoBuilder 類別

定義

定義新的自訂文化特性,或根據其他文化特性和國家/地區的自訂文化特性。Defines a custom culture that is new or based on another culture and country/region. 自訂的文化特性可安裝在電腦上,隨後供電腦上執行的應用程式使用。The custom culture can be installed on a computer and subsequently used by any application that is running on that computer. 這個類別無法被繼承。This class cannot be inherited.

public ref class CultureAndRegionInfoBuilder sealed
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class CultureAndRegionInfoBuilder
type CultureAndRegionInfoBuilder = class
Public NotInheritable Class CultureAndRegionInfoBuilder
繼承
CultureAndRegionInfoBuilder
屬性

範例

下列範例會定義自訂 ru-美國文化特性, 代表美國的俄文語言。The following example defines a custom ru-US culture that represents the Russian language in the United States. 此範例會從俄文 (俄羅斯) CultureInfo物件和美國RegionInfo物件載入設定, 以定義自訂文化CultureAndRegionInfoBuilder特性, 然後設定一些屬性。The example defines the custom culture by loading settings from the Russian (Russia) CultureInfo object and the U.S. RegionInfo object, and then sets a number of CultureAndRegionInfoBuilder properties. 此範例會註冊自訂文化特性, 然後將它具現化, 並使其成為目前的執行緒文化特性。The example registers the custom culture, and then instantiates it and makes it the current thread culture.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Create a custom culture for ru-US.
      CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", 
                                             CultureAndRegionModifiers.None);
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
      car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));
      
      car1.CultureEnglishName = "Russian (United States)";
      car1.CultureNativeName = "русский (США)";
      car1.CurrencyNativeName = "Доллар (США)";
      car1.RegionNativeName = "США";

      // Register the culture.
      try {
         car1.Register();
      }    
      catch (InvalidOperationException) {
         // Swallow the exception: the culture already is registered.
      }
      
      // Use the custom culture.
      CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US");
      Thread.CurrentThread.CurrentCulture = ci;
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo);
   }
}
// The example displays the following output:
//     Current Culture: ru-US
//     Writing System: TextInfo - ru-US
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Create a custom culture for ru-US.
      Dim car1 As New CultureAndRegionInfoBuilder("ru-US", CultureAndRegionModifiers.None)
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"))
      car1.LoadDataFromRegionInfo(New RegionInfo("en-US"))
      
      car1.CultureEnglishName = "Russian (United States)"
      car1.CultureNativeName = "русский (США)"
      car1.CurrencyNativeName = "Доллар (США)"
      car1.RegionNativeName = "США"

      ' Register the culture.
      Try
         car1.Register()
      Catch e As InvalidOperationException
         ' Swallow the exception: the culture already is registered.
      End Try
      
      ' Use the custom culture.
      Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("ru-US")
      Thread.CurrentThread.CurrentCulture = ci
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo)
   End Sub
End Module
' The example displays the following output:
'     Current Culture: ru-US
'     Writing System: TextInfo - ru-US

備註

CultureInfo類別會保存特定文化特性的資訊, 例如相關聯的語言、子語言、國家/地區、行事曆和文化慣例。The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions. 這個類別也會DateTimeFormatInfo提供、 NumberFormatInfoCompareInfoTextInfo類別的文化特性特定實例, 這些是文化特性特定作業 (例如大小寫、格式化和剖析日期和數位) 所需的, 以及比較字串。This class also provides culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo classes, which are required for culture-specific operations such as casing, formatting and parsing dates and numbers, and comparing strings.

根據預設, .NET Framework 支援CultureInfo代表一組預先定義之文化特性的物件。By default, the .NET Framework supports CultureInfo objects that represent a predefined set of cultures. 如需 Windows 系統上可用的這些文化特性清單, 請參閱windows 支援的語言/區功能變數名稱稱清單中的語言標記一欄。For a list of these cultures available on Windows systems, see the Language tag column in the list of language/region names supported by Windows. 文化名稱遵循 BCP 47 定義的標準。Culture names follow the standard defined by BCP 47. CultureAndRegionInfoBuilder類別可讓您建立全新的自訂文化特性, 或覆寫預先定義的文化特性。The CultureAndRegionInfoBuilder class enables you to create a custom culture that is completely new or that overrides a predefined culture. 在特定電腦上安裝並註冊自訂文化特性時, 會與預先定義CultureInfo的物件變得不區分, 而且可以具現化和使用, 就像那些物件一樣。When a custom culture is installed and registered on a particular computer, it becomes indistinguishable from predefined CultureInfo objects, and can be instantiated and used just like those objects.

重要

請注意, CultureAndRegionInfoBuilder類別是在名為 sysglobl 的元件中找到。Note that the CultureAndRegionInfoBuilder class is found in an assembly named sysglobl.dll. 若要成功編譯使用此類型的程式碼, 您必須加入 sysglobl 的參考。Successfully compiling code that uses this type requires that you add a reference to sysglobl.dll.

只有在該電腦上具有系統管理許可權的使用者, 才能在電腦上註冊自訂文化特性。A custom culture can be registered on a computer only by a user who has administrative rights on that computer. 因此, 應用程式通常不會建立及安裝自訂文化特性。Consequently, apps typically do not create and install custom cultures. 相反地, 您可以使用CultureAndRegionInfoBuilder類別來建立特殊用途的工具, 讓系統管理員可以用來建立、安裝及註冊自訂文化特性。Instead, you can use the CultureAndRegionInfoBuilder class to create a special-purpose tool that an administrator can use to create, install, and register a custom culture. 在電腦上註冊自訂文化特性之後, 您可以在應用CultureInfo程式中使用類別來建立自訂文化特性的實例, 就像預先定義的文化特性一樣。After the custom culture is registered on a computer, you can use the CultureInfo class in your app to create instances of the custom culture just as you would for a predefined culture.

如果您剖析針對自訂文化特性產生的日期和時間字串, 您應該DateTime.ParseExact使用DateTime.TryParseExact或方法, 而DateTime.ParseDateTime.TryParse是或方法, 以改善剖析作業成功的機率。If you parse date and time strings generated for a custom culture, you should use the DateTime.ParseExact or DateTime.TryParseExact method instead of the DateTime.Parse or DateTime.TryParse method to improve the probability that the parse operation will succeed. 自訂文化特性的日期和時間字串可能很複雜, 因此難以剖析。A date and time string for a custom culture can be complicated and therefore difficult to parse. ParseTryParse方法會嘗試剖析具有數個隱含剖析模式的字串, 而這兩者都可能會失敗。The Parse and TryParse methods try to parse a string with several implicit parse patterns, all of which might fail. 相反地, 方法會要求應用程式明確指定一或多個可能成功的確切剖析模式。 TryParseExactThe TryParseExact method, in contrast, requires the application to explicitly designate one or more exact parse patterns that are likely to succeed.

定義和建立自訂文化特性Defining and Creating a Custom Culture

您可以使用CultureAndRegionInfoBuilder類別來定義和命名自訂文化特性。You use the CultureAndRegionInfoBuilder class to define and name a custom culture. 自訂文化特性可以是全新的文化特性、以現有文化特性為基礎的新文化特性 (也就是補充文化特性), 或是取代現有 .NET Framework 文化特性的文化特性。The custom culture can be an entirely new culture, a new culture that is based on an existing culture (that is, a supplemental culture), or a culture that replaces an existing .NET Framework culture. 在每個案例中, 基本步驟都相同:In each case, the basic steps are the same:

  1. 藉由呼叫其CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers)方法來具現化物件。CultureAndRegionInfoBuilderInstantiate a CultureAndRegionInfoBuilder object by calling its CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers) constructor. 若要取代現有的文化特性, 請將該文化特性CultureAndRegionModifiers.Replacement的名稱和列舉值傳遞給此函式。To replace an existing culture, pass that culture's name and the CultureAndRegionModifiers.Replacement enumeration value to the constructor. 若要建立新的文化特性或補充文化特性, 請傳遞唯一的文化特性CultureAndRegionModifiers.Neutral名稱CultureAndRegionModifiers.None和或列舉值。To create a new culture or a supplemental culture, pass a unique culture name and either the CultureAndRegionModifiers.Neutral or CultureAndRegionModifiers.None enumeration value.

    注意

    如果您使用CultureAndRegionModifiers.Replacement列舉值來具現CultureAndRegionInfoBuilder化物件, CultureAndRegionInfoBuilder物件的CultureInfo屬性會自動填入要取代之物件中的值。If you use the CultureAndRegionModifiers.Replacement enumeration value to instantiate a CultureAndRegionInfoBuilder object, the CultureAndRegionInfoBuilder object's properties are automatically populated with values from the CultureInfo object to be replaced.

  2. 如果您要建立新的或補充的文化特性:If you are creating a new or supplemental culture:

  3. 視需要修改CultureAndRegionInfoBuilder物件的屬性。Modify the properties of the CultureAndRegionInfoBuilder object as necessary.

  4. 如果您打算在不同的常式中註冊自訂文化特性, 請呼叫Save方法。If you are planning to register the custom culture in a separate routine, call the Save method. 這會產生 XML 檔案, 您可以在個別的自訂文化特性安裝常式中載入並註冊該檔案。This generates an XML file that you can load and register in a separate custom culture installation routine.

註冊自訂文化特性Registering a Custom Culture

如果您要開發的自訂文化特性與建立文化特性的應用程式不同, 您可以呼叫CreateFromLdml方法來載入包含自訂文化特性定義的 XML 檔案, 並具現化CultureAndRegionInfoBuilder物件。If you are developing a registration application for a custom culture that is separate from the application that creates the culture, you call the CreateFromLdml method to load the XML file that contains the custom culture's definition and instantiate the CultureAndRegionInfoBuilder object. 若要處理註冊, 請呼叫Register方法。To handle the registration, call the Register method. 若要成功註冊, 註冊自訂文化特性的應用程式必須在目標系統上以系統管理許可權執行;否則, 呼叫Register UnauthorizedAccessException會擲回例外狀況。For the registration to succeed, the application that registers the custom culture must be running with administrative privileges on the target system; otherwise, the call to Register throws an UnauthorizedAccessException exception.

警告

文化特性資料在系統之間可能有所不同。Culture data can differ between systems. 如果您使用CultureAndRegionInfoBuilder類別來建立在多個系統之間統一的自訂文化特性, 而且您要藉由從現有CultureInfo的和RegionInfo物件載入資料並加以自訂來建立自訂文化特性, 您應該開發兩個不同的公用程式。If you are using the CultureAndRegionInfoBuilder class to create a custom culture that is uniform across multiple systems and you are creating your custom culture by loading data from existing CultureInfo and RegionInfo objects and customizing it, you should develop two different utilities. 第一個會建立自訂文化特性, 並將其儲存至 XML 檔案。The first creates the custom culture and saves it to an XML file. 第二個使用CreateFromLdml方法, 從 XML 檔案載入自訂文化特性, 並在目的電腦上註冊它。The second uses the CreateFromLdml method to load the custom culture from an XML file and register it on the target computer.

註冊程式會執行下列工作:The registration process performs the following tasks:

  • 建立 nlp 檔案, 其中包含在CultureAndRegionInfoBuilder物件中定義的資訊。Creates an .nlp file that contains the information that is defined in the CultureAndRegionInfoBuilder object.

  • 將 nlp 檔案儲存在目的電腦上的%windir%\Globalization 系統目錄中。Stores the .nlp file in the %windir%\Globalization system directory on the target computer. 這可讓自訂文化特性的設定在會話之間保存。This enables the custom culture's settings to persist between sessions. (此CultureAndRegionInfoBuilder方法需要系統管理員許可權, 因為 nlp 檔案儲存在系統目錄中)。(The CultureAndRegionInfoBuilder method requires administrative privileges because the .nlp file is stored in a system directory.)

  • 準備 .NET Framework 在下一次建立新的自訂文化特性的要求時, 搜尋%windir%\Globalization 系統目錄, 而不是內部快取。Prepares the .NET Framework to search the %windir%\Globalization system directory instead of an internal cache the next time there is a request to create your new custom culture.

成功註冊自訂文化特性時, 與 .NET Framework 預先定義的文化特性不區分。When a custom culture is successfully registered, it is indistinguishable from the cultures that are predefined by the .NET Framework. 在呼叫CultureAndRegionInfoBuilder方法之後, 才可以使用自訂文化特性, 從本機電腦中移除 nlp 檔案。The custom culture is available until a call to the CultureAndRegionInfoBuilder method removes the .nlp file from the local computer.

具現化自訂文化特性Instantiating a Custom Culture

您可以使用下列其中一種方式來建立自訂文化特性的實例:You can create an instance of the custom culture in one of the following ways:

此外, CultureInfo.GetCultures方法所傳回的CultureInfo物件陣列也包含自訂文化特性。In addition, the array of CultureInfo objects that is returned by the CultureInfo.GetCultures method includes the custom culture.

建構函式

CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers)

初始化 CultureAndRegionInfoBuilder 類別的新執行個體。Initializes a new instance of the CultureAndRegionInfoBuilder class.

屬性

AvailableCalendars

取得或設定此 CultureAndRegionInfoBuilder 物件所支援之曆法的陣列。Gets or sets an array of calendars that are supported by this CultureAndRegionInfoBuilder object.

CompareInfo

取得或設定 CompareInfo 物件,它定義如何比較文化特性的字串。Gets or sets the CompareInfo object that defines how to compare strings for the culture.

ConsoleFallbackUICulture

當預設的圖形使用者介面不適當時,為主控台應用程式取得或設定適合的替代使用者介面文化特性。Gets or sets an alternate user interface culture suitable for console applications when the default graphic user interface culture is inappropriate.

CultureEnglishName

取得或設定英文的文化特性名稱。Gets or sets the culture name in English.

CultureName

取得將建立之文化特性的名稱。Gets the name of the culture being created.

CultureNativeName

取得或設定文化特性名稱,使用文化特性設定所要顯示的格式和語言。Gets or sets the culture name in the format and language that the culture is set to display.

CultureTypes

取得 CultureTypes 值,它描述目前 CultureAndRegionInfoBuilder 物件所表示的文化特性。Gets the CultureTypes value that describes the culture represented by the current CultureAndRegionInfoBuilder object.

CurrencyEnglishName

取得或設定英文的貨幣名稱,這個貨幣是用於目前 CultureAndRegionInfoBuilder 物件所表示的國家/地區。Gets or sets the name, in English, of the currency used in the country/region represented by the current CultureAndRegionInfoBuilder object.

CurrencyNativeName

取得或設定貨幣的原生名稱,這個貨幣是用於目前 CultureAndRegionInfoBuilder 物件所表示的國家/地區。Gets or sets the native name of the currency used in the country/region represented by the current CultureAndRegionInfoBuilder object.

GeoId

取得或設定地區、國家、縣市或位置的唯一識別碼。Gets or sets a unique identification number for a geographical region, country, city, or location.

GregorianDateTimeFormat

取得或設定 DateTimeFormatInfo 物件,此物件會根據西曆定義日期和時間的格式。Gets or sets a DateTimeFormatInfo object that defines the format of dates and times according to the Gregorian calendar.

IetfLanguageTag

取得或設定文化特性名稱,其格式是根據 RFC 4646 標準<Tags for the Identification of Languages>。Gets or sets a culture name formatted according to the RFC 4646 standard, "Tags for the Identification of Languages."

IsMetric

取得或設定值,指出國家/地區是否使用公制系統為度量。Gets or sets a value indicating whether the country/region uses the metric system for measurements.

ISOCurrencySymbol

取得或設定與國家/地區關聯之三個字元的 ISO 4217 貨幣符號。Gets or sets the three-character ISO 4217 currency symbol associated with the country/region.

IsRightToLeft

取得或設定與目前 CultureAndRegionInfoBuilder 物件關聯之書寫系統中文字行的主要方向。Gets or sets the predominant direction of lines of text in the writing system associated with the current CultureAndRegionInfoBuilder object.

KeyboardLayoutId

取得或設定使用中的輸入地區設定識別項。Gets or sets the active input locale identifier.

LCID

取得目前 CultureAndRegionInfoBuilder 物件的文化特性識別項。Gets the culture identifier for the current CultureAndRegionInfoBuilder object.

NumberFormat

取得或設定 NumberFormatInfo 物件,定義數字、貨幣和百分比在文化特性上適當的顯示格式。Gets or sets a NumberFormatInfo object that defines the culturally appropriate format of displaying numbers, currency, and percentage.

Parent

取得或設定 CultureInfo 物件,表示目前自訂文化特性的父文化特性。Gets or sets the CultureInfo object that represents the parent culture of the current custom culture.

RegionEnglishName

取得或設定國家/地區的完整英文名稱。Gets or sets the full name of the country/region in English.

RegionName

為目前的 CultureAndRegionInfoBuilder 物件取得國家/地區名稱。Gets the name of the country/region for the current CultureAndRegionInfoBuilder object.

RegionNativeName

取得或設定此自訂文化特性為人們熟知的國家/地區全名。Gets or sets the full name of the country/region as known by the people of this custom culture.

TextInfo

取得或設定 TextInfo 物件,它定義與此自訂文化特性相關的書寫系統。Gets or sets the TextInfo object that defines the writing system associated with this custom culture.

ThreeLetterISOLanguageName

取得或設定此自訂文化特性之語言的 ISO 639-2 三個字母代碼。Gets or sets the ISO 639-2 three-letter code for the language of this custom culture.

ThreeLetterISORegionName

取得或設定國家/地區在 ISO 3166 中定義的三個字母代碼。Gets or sets the three-letter code defined in ISO 3166 for the country/region.

ThreeLetterWindowsLanguageName

取得或設定在 Windows API 中所定義之語言的三個字母代碼。Gets or sets the three-letter code for the language as defined in the Windows API.

ThreeLetterWindowsRegionName

取得或設定 Windows 指派給由目前自訂文化特性所表示之國家/地區的三個字母代碼。Gets or sets the three-letter code assigned by Windows to the country/region represented by the current custom culture.

TwoLetterISOLanguageName

取得或設定目前 CultureInfo 物件之語言的 ISO 639-1 兩個字母代碼。Gets or sets the ISO 639-1 two-letter code for the language of the current CultureInfo object.

TwoLetterISORegionName

取得或設定國家/地區在 ISO 3166 中定義的兩個字母代碼。Gets or sets the two-letter code defined in ISO 3166 for the country/region.

方法

CreateFromLdml(String)

從包含物件表示的指定 XML 檔案,重新組成 CultureAndRegionInfoBuilder 物件。Reconstitutes a CultureAndRegionInfoBuilder object from a specified XML file that contains a representation of the object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
LoadDataFromCultureInfo(CultureInfo)

使用指定之 CultureInfo 物件的對應屬性,設定目前 CultureAndRegionInfoBuilder 物件的屬性。Sets the properties of the current CultureAndRegionInfoBuilder object with the corresponding properties of the specified CultureInfo object.

LoadDataFromRegionInfo(RegionInfo)

使用指定之 RegionInfo 物件的對應屬性,設定目前 CultureAndRegionInfoBuilder 物件的屬性。Sets the properties of the current CultureAndRegionInfoBuilder object with the corresponding properties of the specified RegionInfo object.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
Register()

將目前的 CultureAndRegionInfoBuilder 物件當做自訂文化特性保存在本機電腦上,並讓應用程式可以使用該文化特性。Persists the current CultureAndRegionInfoBuilder object as a custom culture on the local computer and makes that culture available to applications. 必須有系統管理員權限。Requires administrative privileges.

Save(String)

將目前 CultureAndRegionInfoBuilder 物件的 XML 表示寫入指定檔案中。Writes an XML representation of the current CultureAndRegionInfoBuilder object to the specified file.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Unregister(String)

從本機電腦刪除自訂的文化特性。Deletes a custom culture from the local computer.

適用於