CultureInfo.CurrentCulture CultureInfo.CurrentCulture CultureInfo.CurrentCulture CultureInfo.CurrentCulture Property

定義

取得或設定 CultureInfo 物件,這個物件代表目前執行緒使用的文化特性。Gets or sets the CultureInfo object that represents the culture used by the current thread.

public:
 static property System::Globalization::CultureInfo ^ CurrentCulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public static System.Globalization.CultureInfo CurrentCulture { get; set; }
member this.CurrentCulture : System.Globalization.CultureInfo with get, set
Public Shared Property CurrentCulture As CultureInfo

屬性值

物件,代表目前執行緒使用的文化特性。An object that represents the culture used by the current thread.

例外狀況

該屬性設定為 nullThe property is set to null.

範例

下列範例示範如何變更CurrentCultureCurrentUICulture目前執行緒。The following example demonstrates how to change the CurrentCulture and CurrentUICulture of the current thread.

using namespace System;
using namespace System::Globalization;
using namespace System::Threading;

int main()
{
   // Display the name of the current thread culture.
   Console::WriteLine("CurrentCulture is {0}.", CultureInfo::CurrentCulture->Name);
   
   // Change the current culture to th-TH.
   CultureInfo::CurrentCulture = gcnew CultureInfo("th-TH",false);
   Console::WriteLine("CurrentCulture is now {0}.", CultureInfo::CurrentCulture->Name);
   
   // Displays the name of the CurrentUICulture of the current thread.
   Console::WriteLine("CurrentUICulture is {0}.", CultureInfo::CurrentCulture->Name);
   
   // Changes the CurrentUICulture of the current thread to ja-JP.
   CultureInfo::CurrentUICulture = gcnew CultureInfo("ja-JP",false);
   Console::WriteLine("CurrentUICulture is now {0}.", CultureInfo::CurrentCulture->Name);
}
// The example displays the following output:
//       CurrentCulture is en-US.
//       CurrentCulture is now th-TH.
//       CurrentUICulture is en-US.
//       CurrentUICulture is now ja-JP.
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()  
   {
      // Display the name of the current thread culture.
      Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);

      // Change the current culture to th-TH.
      CultureInfo.CurrentCulture = new CultureInfo("th-TH", false);
      Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);

      // Display the name of the current UI culture.
      Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);

      // Change the current UI culture to ja-JP.
      CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false );
      Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
   }
}
// The example displays the following output:
//       CurrentCulture is en-US.
//       CurrentCulture is now th-TH.
//       CurrentUICulture is en-US.
//       CurrentUICulture is now ja-JP.
Imports System.Globalization
Imports System.Threading

Public Module Example
   Public Sub Main()

      ' Display the name of the current thread culture.
      Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name)

      ' Change the current culture to th-TH.
      CultureInfo.CurrentCulture = New CultureInfo("th-TH", False)
      Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name)

      ' Display the name of the current UI culture.
      Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name)

      ' Change the current UI culture to ja-JP.
      CultureInfo.CurrentUICulture = New CultureInfo("ja-JP", False)
      Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name)
   End Sub 
End Module
' The example displays the following output:
'       CurrentCulture is en-US.
'       CurrentCulture is now th-TH.
'       CurrentUICulture is en-US.
'       CurrentUICulture is now ja-JP.

備註

CultureInfo這個屬性會傳回的物件和其相關聯的物件判斷預設格式的日期、 時間、 數字、 貨幣值,排序順序的文字、 大小寫慣例,以及字串比較。The CultureInfo object that is returned by this property and its associated objects determine the default format for dates, times, numbers, currency values, the sorting order of text, casing conventions, and string comparisons.

目前的文化特性是執行中執行緒的屬性。The current culture is a property of the executing thread. 擷取的值CultureInfo.CurrentCulture屬性相當於更多高效能的擷取CultureInfo所傳回的物件Thread.CurrentThread.CurrentCulture屬性。Retrieving the value of the CultureInfo.CurrentCulture property is a more performant equivalent of retrieving the CultureInfo object returned by the Thread.CurrentThread.CurrentCulture property.

注意

在.NET Framework 4.5.2 和舊版中,CurrentCulture屬性是唯讀的; 也就是說,您可以擷取屬性值,但您無法將它設定。In the .NET Framework 4.5.2 and earlier versions, the CurrentCulture property is read-only; that is, you can retrieve the property value, but you cannot set it. 若要變更目前的文化特性,您指派CultureInfo物件,表示新的文化特性Thread.CurrentThread.CurrentCulture屬性。To change the current culture, you assign the CultureInfo object that represents the new culture to the Thread.CurrentThread.CurrentCulture property. 開頭.NET Framework 4.6.NET Framework 4.6,則CurrentCulture屬性是讀寫; 您可設定和擷取屬性的值。Starting with the .NET Framework 4.6.NET Framework 4.6, the CurrentCulture property is read-write; you can both set and retrieve the property's value. 如果您沒有設定屬性值為CultureInfo物件,表示新的文化特性的值Thread.CurrentThread.CurrentCulture屬性也會變更。If you do set the property value to a CultureInfo object that represents a new culture, the value of the Thread.CurrentThread.CurrentCulture property also changes.

本節內容:In this section:

如何判斷執行緒的文化特性 How a Thread's Culture Is Determined
取得目前的文化特性 Getting the Current Culture
明確設定 CurrentCulture 屬性 Explicitly Setting the CurrentCulture Property
目前的文化特性和使用者覆寫 The Current Culture and User Overrides
文化特性和 Windows 應用程式Culture and Windows Apps

如何判斷執行緒的文化特性How a Thread's Culture Is Determined

在執行緒啟動時,其文化特性是一開始決定,如下所示:When a thread is started, its culture is initially determined as follows:

  • 藉由擷取所指定的文化特性DefaultThreadCurrentCulture執行緒正在其中執行,如果屬性值不是應用程式定義域中的屬性nullBy retrieving the culture that is specified by the DefaultThreadCurrentCulture property in the application domain in which the thread is executing, if the property value is not null.

  • 如果執行緒集區執行緒執行工作為基礎的非同步作業和應用程式目標.NET Framework 4.6.NET Framework 4.6或更新版本的.NET Framework 中,其文化特性取決於呼叫執行緒的文化特性。If the thread is a thread pool thread that is executing a task-based asynchronous operation and the app targets the .NET Framework 4.6.NET Framework 4.6 or a later version of the .NET Framework, its culture is determined by the culture of the calling thread. 下列範例將目前的文化特性變更為葡萄牙文 (巴西),並啟動六個工作,其中每一個會顯示其執行緒 ID、 其工作識別碼和其目前的文化特性。The following example changes the current culture to Portuguese (Brazil) and launches six tasks, each of which displays its thread ID, its task ID, and its current culture. 每個工作 (執行緒) 有繼承呼叫執行緒的文化特性。Each of the tasks (and the threads) has inherited the culture of the calling thread.

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Versioning;
    using System.Threading;
    using System.Threading.Tasks;
    
    [assembly:TargetFramework(".NETFramework,Version=v4.6")]
    
    public class Example
    {
       public static async Task Main()
       {
          var tasks = new List<Task>();
          Console.WriteLine("The current culture is {0}", 
                            Thread.CurrentThread.CurrentCulture.Name);
          Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
          // Change the current culture to Portuguese (Brazil).
          Console.WriteLine("Current culture changed to {0}",
                            Thread.CurrentThread.CurrentCulture.Name);
          Console.WriteLine("Application thread is thread {0}",
                            Thread.CurrentThread.ManagedThreadId);
          // Launch six tasks and display their current culture.
          for (int ctr = 0; ctr <= 5; ctr++)
             tasks.Add(Task.Run( () => {
                                   Console.WriteLine("Culture of task {0} on thread {1} is {2}",
                                                     Task.CurrentId, 
                                                     Thread.CurrentThread.ManagedThreadId,
                                                     Thread.CurrentThread.CurrentCulture.Name);
                                } ));                     
    
          await Task.WhenAll(tasks.ToArray());
       }
    }
    // The example displays output like the following:
    //     The current culture is en-US
    //     Current culture changed to pt-BR
    //     Application thread is thread 9
    //     Culture of task 2 on thread 11 is pt-BR
    //     Culture of task 1 on thread 10 is pt-BR
    //     Culture of task 3 on thread 11 is pt-BR
    //     Culture of task 5 on thread 11 is pt-BR
    //     Culture of task 6 on thread 11 is pt-BR
    //     Culture of task 4 on thread 10 is pt-BR
    
    Imports System.Collections.Generic
    Imports System.Globalization
    Imports System.Runtime.Versioning
    Imports System.Threading
    Imports System.Threading.Tasks
    
    <assembly:TargetFramework(".NETFramework,Version=v4.6")>
    
    Module Example
       Public Sub Main()
          Dim tasks As New List(Of Task)
          Console.WriteLine("The current culture is {0}", 
                            Thread.CurrentThread.CurrentCulture.Name)
          Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-BR")
          ' Change the current culture to Portuguese (Brazil).
          Console.WriteLine("Current culture changed to {0}",
                            Thread.CurrentThread.CurrentCulture.Name)
          Console.WriteLine("Application thread is thread {0}",
                            Thread.CurrentThread.ManagedThreadId)
          ' Launch six tasks and display their current culture.
          For ctr As Integer = 0 to 5
             tasks.Add(Task.Run(Sub()
                                   Console.WriteLine("Culture of task {0} on thread {1} is {2}",
                                                     Task.CurrentId, 
                                                     Thread.CurrentThread.ManagedThreadId,
                                                     Thread.CurrentThread.CurrentCulture.Name)
                                End Sub))                     
          Next
          Task.WaitAll(tasks.ToArray())
       End Sub
    End Module
    ' The example displays output like the following:
    '     The current culture is en-US
    '     Current culture changed to pt-BR
    '     Application thread is thread 9
    '     Culture of task 2 on thread 11 is pt-BR
    '     Culture of task 1 on thread 10 is pt-BR
    '     Culture of task 3 on thread 11 is pt-BR
    '     Culture of task 5 on thread 11 is pt-BR
    '     Culture of task 6 on thread 11 is pt-BR
    '     Culture of task 4 on thread 10 is pt-BR
    

    如需詳細資訊,請參閱 「 文化特性和以工作為基礎的非同步作業 > 一節中CultureInfo主題。For more information, see the "Culture and task-based asynchronous operations" section in the CultureInfo topic.

  • 藉由呼叫 WindowsGetUserDefaultLocaleName函式。By calling the Windows GetUserDefaultLocaleName function.

請注意,是否您設定特定的文化特性不同的系統安裝的文化特性或使用者的慣用文化特性,而且您的應用程式會啟動多個執行緒,這些執行緒的目前文化特性,將會是所傳回的文化特性GetUserDefaultLocaleName函式,除非您將指定的文化特性DefaultThreadCurrentCulture執行緒正在執行的應用程式定義域中的屬性。Note that if you set a specific culture that is different from the system-installed culture or the user's preferred culture, and your application starts multiple threads, the current culture of those threads will be the culture that is returned by the GetUserDefaultLocaleName function, unless you assign a culture to the DefaultThreadCurrentCulture property in the application domain in which the thread is executing.

如需有關如何判斷執行緒的文化特性的詳細資訊,請參閱中的 < 文化特性和執行緒 > 一節CultureInfo參考頁面。For more information about how the culture of a thread is determined, see the "Culture and threads" section in the CultureInfo reference page.

取得目前的文化特性Getting the Current Culture

CultureInfo.CurrentCulture屬性是每個執行緒設定,也就是每個執行緒可以有它自己的文化特性。The CultureInfo.CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. 您所擷取的值取得目前執行緒的文化特性CultureInfo.CurrentCulture屬性,如下列範例說明。You get the culture of the current thread by retrieving the value of the CultureInfo.CurrentCulture property, as the following example illustrates.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CurrentCulture;
      Console.WriteLine("The current culture is {0} [{1}]",
                        culture.NativeName, culture.Name);
   }
}
// The example displays output like the following:
//       The current culture is English (United States) [en-US]
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim culture As CultureInfo = CultureInfo.CurrentCulture
      Console.WriteLine("The current culture is {0} [{1}]",
                        culture.NativeName, culture.Name)
   End Sub
End Module
' The example displays output like the following:
'     The current culture is English (United States) [en-US]

您也可以擷取從目前執行緒的文化特性值Thread.CurrentThread屬性。You can also retrieve the value of the current thread's culture from the Thread.CurrentThread property.

明確設定 CurrentCulture 屬性Explicitly Setting the CurrentCulture Property

繼續.NET Framework 4.6.NET Framework 4.6,以變更現有的執行緒,使用的文化特性設定CultureInfo.CurrentCulture為新的文化特性的屬性。Staring with the .NET Framework 4.6.NET Framework 4.6, to change the culture that is used by an existing thread, you set the CultureInfo.CurrentCulture property to the new culture. 如果您明確變更執行緒的文化特性,如此一來,變更會保存如果執行緒跨越應用程式定義域界限。If you explicitly change a thread's culture in this way, that change persists if the thread crosses application domain boundaries. 下列範例會變更目前執行緒文化特性為荷蘭文 (荷蘭)。The following example changes the current thread culture to Dutch (Netherlands). 它也會顯示,當目前的執行緒跨越應用程式定義域界限,其目前的文化特性會保持變更。It also shows that, when the current thread crosses application domain boundaries, its current culture remains changed.

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

public class Info : MarshalByRefObject
{
   public void ShowCurrentCulture()
   {
      Console.WriteLine("Culture of {0} in application domain {1}: {2}",
                        Thread.CurrentThread.Name,
                        AppDomain.CurrentDomain.FriendlyName,
                        CultureInfo.CurrentCulture.Name);
   }
}

public class Example
{
   public static void Main()
   {
      Info inf = new Info();
      // Set the current culture to Dutch (Netherlands).
      Thread.CurrentThread.Name = "MainThread";
      CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
      inf.ShowCurrentCulture();
      
      // Create a new application domain.
       AppDomain ad = AppDomain.CreateDomain("Domain2");
       Info inf2 = (Info) ad.CreateInstanceAndUnwrap(typeof(Info).Assembly.FullName, "Info");
       inf2.ShowCurrentCulture();                       
   }
}
// The example displays the following output:
//       Culture of MainThread in application domain ChangeCulture1.exe: nl-NL
//       Culture of MainThread in application domain Domain2: nl-NL
Imports System.Globalization
Imports System.Threading

Public Class Info : Inherits MarshalByRefObject
   Public Sub ShowCurrentCulture()
      Console.WriteLine("Culture of {0} in application domain {1}: {2}",
                        Thread.CurrentThread.Name,
                        AppDomain.CurrentDomain.FriendlyName,
                        CultureInfo.CurrentCulture.Name)
   End Sub
End Class

Module Example
   Public Sub Main()
      Dim inf As New Info()
      ' Set the current culture to Dutch (Netherlands).
      Thread.CurrentThread.Name = "MainThread"
      CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL")
      inf.ShowCurrentCulture()
      
      ' Create a new application domain.
       Dim ad As AppDomain = AppDomain.CreateDomain("Domain2")
       Dim inf2 As Info = CType(ad.CreateInstanceAndUnwrap(GetType(Info).Assembly.FullName, "Info"),
                          Info)  
       inf2.ShowCurrentCulture()                       
   End Sub
End Module
' This example displays the following output:
'       Culture of MainThread in application domain Example.exe: nl-NL
'       Culture of MainThread in application domain Domain2: nl-NL

注意

變更使用的文化特性CultureInfo.CurrentCulture屬性需要SecurityPermission具有權限ControlThread值的設定。Changing the culture by using the CultureInfo.CurrentCulture property requires a SecurityPermission permission with the ControlThread value set. 操作執行緒有危險,因為與執行緒相關聯的安全性狀態。Manipulating threads is dangerous because of the security state associated with threads. 因此,只有可靠的程式碼,然後必要時,才應指定此權限。Therefore, this permission should be given only to trustworthy code, and then only as necessary. 您無法變更非完全信任的程式碼中的執行緒文化特性。You cannot change thread culture in semi-trusted code.

從開始.NET Framework 4.NET Framework 4,您可以明確地變更目前執行緒文化特性 (例如,法文 (加拿大)) 的特定文化特性或中性文化特性 (例如,法文)。Starting with the .NET Framework 4.NET Framework 4, you can explicitly change the current thread culture to either a specific culture (such as French (Canada)) or a neutral culture (such as French). CultureInfo物件都代表中性文化特性的值CultureInfo屬性,例如CalendarCompareInfoDateTimeFormatNumberFormat,和TextInfo反映相關聯的特定文化特性中性文化特性。When a CultureInfo object represents a neutral culture, the values of CultureInfo properties such as Calendar, CompareInfo, DateTimeFormat, NumberFormat, and TextInfo reflect the specific culture that is associated with the neutral culture. 例如,英文的中性文化特性的主要文化特性是英文 (美國);主控項的文化特性,德文文化特性是德文 (德國)。For example, the dominant culture for the English neutral culture is English (United States); the dominant culture for the German culture is German (Germany). 下列範例說明格式化當目前的文化特性設定為特定文化特性、 法文 (加拿大) 和中性文化特性,差異法文。The following example illustrates the difference in formatting when the current culture is set to a specific culture, French (Canada), and a neutral culture, French.

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

public class Example
{
   public static void Main()
   {
      double value = 1634.92;
      CultureInfo.CurrentCulture = new CultureInfo("fr-CA");
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name);
      Console.WriteLine("{0:C2}\n", value);
      
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr");
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name);
      Console.WriteLine("{0:C2}", value);
   }
}
// The example displays the following output:
//       Current Culture: fr-CA
//       1 634,92 $
//       
//       Current Culture: fr
//       1 634,92 €
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim value As Double = 1634.92
      CultureInfo.CurrentCulture = New CultureInfo("fr-CA")
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name)
      Console.WriteLine("{0:C2}", value)
      Console.WriteLine()
      
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr")
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name)
      Console.WriteLine("{0:C2}", value)
   End Sub
End Module
' The example displays the following output:
'       Current Culture: fr-CA
'       1 634,92 $
'       
'       Current Culture: fr
'       1 634,92 €

您也可以使用CultureInfo.CurrentCulture屬性連同HttpRequest.UserLanguages屬性來設定CurrentCulture屬性的 ASP.NET 應用程式給使用者的慣用文化特性,如下列範例所示。You can also use the CultureInfo.CurrentCulture property along with the HttpRequest.UserLanguages property to set the CurrentCulture property of an ASP.NET application to the user's preferred culture, as the following example illustrates.

CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))

目前的文化特性和使用者覆寫The Current Culture and User Overrides

Windows 可讓使用者覆寫的標準屬性值CultureInfo物件和其相關聯的物件,使用地區及語言選項控制項台中。Windows allows users to override the standard property values of the CultureInfo object and its associated objects by using Regional and Language Options in Control Panel. CultureInfo所傳回的物件CurrentCulture屬性會反映這些使用者覆寫在下列情況:The CultureInfo object returned by the CurrentCulture property reflects these user overrides in the following cases:

  • 如果由 Windows 隱含設定目前執行緒文化特性GetUserDefaultLocaleName函式。If the current thread culture is set implicitly by the Windows GetUserDefaultLocaleName function.

  • 如果目前執行緒文化特性所定義DefaultThreadCurrentCulture屬性會對應至目前的 Windows 系統文化特性。If the current thread culture defined by the DefaultThreadCurrentCulture property corresponds to the current Windows system culture.

  • 如果目前執行緒文化特性設定明確地為所傳回的文化特性CreateSpecificCulture方法和文化特性與目前的 Windows 系統文化特性。If the current thread culture is set explicitly to a culture returned by the CreateSpecificCulture method, and that culture corresponds to the current Windows system culture.

  • 如果目前執行緒文化特性設明確具現化的文化特性CultureInfo(String)建構函式,和文化特性與目前的 Windows 系統文化特性。If the current thread culture is set explicitly to a culture instantiated by the CultureInfo(String) constructor, and that culture corresponds to the current Windows system culture.

在某些情況下,特別是針對伺服器應用程式,將目前的文化特性設定為CultureInfo可能不會反映使用者覆寫的物件。In some cases, particularly for server applications, setting the current culture to a CultureInfo object that reflects user overrides may be undesirable. 相反地,您可以將目前執行緒文化特性設定為CultureInfo不會反映使用者的物件會覆寫,以下列方式:Instead, you can set the current thread culture to a CultureInfo object that does not reflect user overrides in the following ways:

目前的文化特性和 UWP 應用程式The current culture and UWP apps

在通用 Windows 平台 (UWP) 應用程式,CurrentCulture屬性是讀寫,就如同它是.NET Framework 和.NET Core 應用程式中,您可以使用它來取得和設定目前的文化特性。In Universal Windows Platform (UWP) apps, the CurrentCulture property is read-write, just as it is in .NET Framework and .NET Core apps; you can use it both to get and to set the current culture. 不過,UWP 應用程式無法區分目前的文化特性與目前的 UI 文化特性。However, UWP apps do not distinguish between the current culture and the current UI culture. CurrentCultureCurrentUICulture中的第一個值的屬性對應Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages集合。The CurrentCulture and CurrentUICulture properties map to the first value in the Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages collection.

在.NET Framework 和.NET Core 應用程式中目前的文化特性是每個執行緒設定,而CurrentCulture屬性會反映目前執行緒的文化特性。In .NET Framework and .NET Core apps, the current culture is a per-thread setting, and the CurrentCulture property reflects the culture of the current thread only. 在 UWP 應用程式,目前的文化特性會對應至Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages屬性,這是一種全域設定。In UWP apps, the current culture maps to the Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages property, which is a global setting. 設定CurrentCulture屬性也會變更整個應用程式的文化特性; 無法設定每個執行緒為基礎的文化特性。Setting the CurrentCulture property changes the culture of the entire app; culture cannot be set on a per-thread basis.

安全性

SecurityPermission
用來設定目前的文化特性。for setting the current culture. 相關聯的列舉型別: ControlThreadAssociated enumeration: ControlThread

適用於

另請參閱