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(); };
public static System.Globalization.CultureInfo CurrentCulture { get; }
member this.CurrentCulture : System.Globalization.CultureInfo
Public Shared ReadOnly Property CurrentCulture As CultureInfo

屬性值

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

例外狀況

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

範例

下列範例示範如何變更目前線程的CurrentCultureCurrentUICultureThe 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屬性的值比取得Thread.CurrentThread.CurrentCulture屬性所傳回的CultureInfo物件更具效能。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域中,抓取屬性所指定的文化特性(如果屬性值不null是)。By 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. 下列範例會將目前文化特性變更為葡萄牙文(巴西)並啟動六個工作,每個工作都會顯示其執行緒識別碼、工作識別碼和目前的文化特性。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.

  • 藉由呼叫 Windows GetUserDefaultLocaleName函式。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 4Starting 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). DateTimeFormat Calendar CultureInfo TextInfo NumberFormat當物件代表中性文化特性時,屬性的值(例如、 CompareInfo、、和)會反映與相關聯的特定文化特性。 CultureInfo中性文化特性。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屬性使用屬性,將 ASP.NET 應用程式的CurrentCulture屬性設定為使用者的慣用文化特性,如下列範例所示。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. CurrentCulture屬性CultureInfo所傳回的物件會在下列情況下反映這些使用者覆寫: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. 和屬性會對應到 DefaultCoNtext 中的第一個值。 ApplicationModel 的集合。 CurrentCulture CurrentUICultureThe 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 應用程式中,目前的文化特性會對應到DefaultCoNtext 的 ApplicationModel屬性,這是一個全域設定。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

適用於

另請參閱