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.

示例

下面的示例演示如何更改当前线程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.

注解

此属性返回的对象及其关联的对象确定日期、时间、数字、货币值的默认格式、文本的排序顺序、大小写约定和字符串比较。CultureInfoThe 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检索属性指定的区域性,如果属性值不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. 下面的示例将当前区域性更改为葡萄牙语(巴西)并启动六个任务,每个任务显示其线程 ID、任务 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.

  • 通过调用 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

起始在CultureInfo.CurrentCulture中,若要更改现有线程使用的区域性,请将属性设置为新的区域性。 .NET Framework 4.6.NET Framework 4.6Staring 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). CultureInfo Calendar TextInfo CompareInfo NumberFormat当对象表示非特定区域性时,DateTimeFormat属性的值(如、、、和)反映与关联的特定区域性。 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 集合中的第一个值。 windows.applicationmodel.resources.core 集合中的第一个值。 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 应用中,当前区域性映射到windows.applicationmodel.resources.core属性,这是一项全局设置,它是一个全局设置。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

适用于

另请参阅