CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture CultureInfo.CurrentUICulture Property

定义

获取或设置 CultureInfo 对象,该对象表示资源管理器在运行时查找区域性特定资源时所用的当前用户接口区域性。Gets or sets the CultureInfo object that represents the current user interface culture used by the Resource Manager to look up culture-specific resources at run time.

public:
 static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentUICulture { get; }
member this.CurrentUICulture : System.Globalization.CultureInfo
Public Shared ReadOnly Property CurrentUICulture As CultureInfo

属性值

资源管理器用于在运行时查找查找区域性特定资源的区域性。The culture used by the Resource Manager to look up culture-specific resources at run time.

异常

属性设置为 nullThe property is set to null.

该属性设置为不能用于定位资源文件的区域性名称。The property is set to a culture name that cannot be used to locate a resource file. 资源文件名必须仅包含字母、数字、连字符或下划线。Resource filenames can include only letters, numbers, hyphens, or underscores.

示例

下面的代码示例演示如何更改当前线程CurrentCultureCurrentUICulture和。The following code 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.

注解

当前 UI 区域性为每个线程的属性。The current UI culture is a per-thread property. 也就是说,每个线程都有自己的当前 UI 区域性。That is, each thread has its own current UI culture. 此属性等效于检索或,从开始.NET Framework 4.6.NET Framework 4.6,设置分配给System.Threading.Thread.CurrentThread.CurrentUICulture属性CultureInfo的对象。This property is equivalent to retrieving or, starting with the .NET Framework 4.6.NET Framework 4.6, setting the CultureInfo object assigned to the System.Threading.Thread.CurrentThread.CurrentUICulture property. 启动线程时,它的 UI 区域性最初确定如下:When a thread is started, its UI culture is initially determined as follows:

  • 通过在执行线程的应用程序域中DefaultThreadCurrentUICulture检索属性指定的区域性,如果属性值不null为。By retrieving the culture that is specified by the DefaultThreadCurrentUICulture 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,则其 ui 区域性由调用线程的 ui 区域性确定。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 UI culture is determined by the UI culture of the calling thread. 下面的示例将当前 UI 区域性更改为葡萄牙语(巴西)并启动六个任务,每个任务显示其线程 ID、任务 ID 及其当前 UI 区域性。The following example changes the current UI culture to Portuguese (Brazil) and launches six tasks, each of which displays its thread ID, its task ID, and its current UI culture. 每个任务(和线程)都继承了调用线程的 UI 区域性。Each of the tasks (and the threads) has inherited the UI 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 UI culture is {0}", 
                            Thread.CurrentThread.CurrentUICulture.Name);
          Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");
          // Change the current UI culture to Portuguese (Brazil).
          Console.WriteLine("Current UI culture changed to {0}",
                            Thread.CurrentThread.CurrentUICulture.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("UI Culture of task {0} on thread {1} is {2}",
                                                     Task.CurrentId, 
                                                     Thread.CurrentThread.ManagedThreadId,
                                                     Thread.CurrentThread.CurrentUICulture.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 UI culture is {0}", 
                            Thread.CurrentThread.CurrentUICulture.Name)
          Thread.CurrentThread.CurrentUICulture = New CultureInfo("pt-BR")
          ' Change the current UI culture to Portuguese (Brazil).
          Console.WriteLine("Current culture changed to {0}",
                            Thread.CurrentThread.CurrentUICulture.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.CurrentUICulture.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 GetUserDefaultUILanguage函数。By calling the Windows GetUserDefaultUILanguage function.

备注

在中CurrentUICulture ,属性是只读的。 .NET Compact Framework.NET Compact FrameworkIn the .NET Compact Framework.NET Compact Framework, the CurrentUICulture property is read-only. 当前 UI 区域性由系统的区域设置确定,不能以编程方式进行更改。The current UI culture is determined by the system's regional settings and cannot be changed programmatically.

从开始Thread.CurrentUICulture ,若要更改线程使用的用户界面区域性,请将属性设置为新的区域性。 .NET Framework 4.6.NET Framework 4.6Starting with the .NET Framework 4.6.NET Framework 4.6, to change the user interface culture used by a thread, set the Thread.CurrentUICulture property to the new culture. 如果以这种方式显式更改线程的 UI 区域性,则当线程跨越应用程序域边界时,此更改将保持不变。If you explicitly change a thread's UI culture in this way, that change persists if the thread crosses application domain boundaries.

备注

在 .NET Framework 4.5.2 及更早版本中, CurrentUICulture属性是只读的; 也就是说,您可以检索属性值,但不能对其进行设置。In the .NET Framework 4.5.2 and earlier versions, the CurrentUICulture property is read-only; that is, you can retrieve the property value, but you cannot set it. 若要更改当前 ui 区域性,请将表示CultureInfo新 ui 区域性的对象分配Thread.CurrentThread.CurrentUICulture给属性。To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread.CurrentThread.CurrentUICulture property. .NET Framework 4.6.NET Framework 4.6从开始CultureInfo.CurrentUICulture ,属性为读写属性; 您可以设置和检索属性的值。Starting with the .NET Framework 4.6.NET Framework 4.6, the CultureInfo.CurrentUICulture 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:

获取当前 UI 区域性 Getting the Current UI Culture
显式设置当前 UI 区域性 Explicitly Setting the Current UI Culture
隐式设置当前 UI 区域性 Implicitly Setting the Current UI Culture
安全注意事项 Security Considerations
当前 UI 区域性和 Windows 应用The Current UI Culture and Windows apps

获取当前 UI 区域性Getting the Current UI Culture

CultureInfo.CurrentUICulture属性是每个线程的设置; 也就是说,每个线程都可以有自己的 UI 区域性。The CultureInfo.CurrentUICulture property is a per-thread setting; that is, each thread can have its own UI culture. 可以通过检索CultureInfo.CurrentUICulture属性的值来获取当前线程的 UI 区域性,如下面的示例所示。You get the UI culture of the current thread by retrieving the value of the CultureInfo.CurrentUICulture property, as the following example illustrates.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CurrentUICulture;
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        culture.NativeName, culture.Name);
   }
}
// The example displays output like the following:
//       The current UI 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 UI culture is {0} [{1}]",
                        culture.NativeName, culture.Name)
   End Sub
End Module
' The example displays output like the following:
'     The current UI culture is English (United States) [en-US]

还可以从Thread.CurrentUICulture属性中检索当前线程的 UI 区域性的值。You can also retrieve the value of the current thread's UI culture from the Thread.CurrentUICulture property.

显式设置当前 UI 区域性Explicitly Setting the Current UI Culture

从开始CultureInfo CultureInfo.CurrentUICulture ,可以通过将表示新区域性的对象分配给属性,来更改当前 UI 区域性。 .NET Framework 4.6.NET Framework 4.6Starting with the .NET Framework 4.6.NET Framework 4.6, you can change the current UI culture by assigning a CultureInfo object that represents the new culture to the CultureInfo.CurrentUICulture property. 当前 UI 区域性可以设置为特定的区域性(例如 en-us 或 de),也可以设置为非特定区域性(如 en 或 de)。The current UI culture can be set to either a specific culture (such as en-US or de-DE) or to a neutral culture (such as en or de). 下面的示例将当前 UI 区域性设置为 fr-fr 或法语(法国)。The following example sets the current UI culture to fr-FR or French (France).

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name);
      
      CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name);
   }
}
// The example displays output like the following:
//       The current UI culture: en-US
//       The current UI culture: fr-FR
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name)
      
      CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR")
      Console.WriteLine("The current UI culture: {0}", 
                        CultureInfo.CurrentUICulture.Name)
   End Sub
End Module
' The example displays output like the following:
'       The current UI culture: en-US
'       The current UI culture: fr-FR

在多线程应用程序中,可以通过将表示区域性的CultureInfo对象分配给线程的Thread.CurrentUICulture属性来显式设置任何线程的 UI 区域性。In a multithreaded application, you can explicitly set the UI culture of any thread by assigning a CultureInfo object that represents that culture to the thread's Thread.CurrentUICulture property. 如果要设置其区域性的线程是当前线程,则可以将新区域性分配给该CultureInfo.CurrentUICulture属性。If the thread whose culture you want to set is the current thread, you can assign the new culture to the CultureInfo.CurrentUICulture property. 当显式设置线程的 UI 区域性时,即使该线程跨越应用程序域边界并在另一个应用程序域中执行代码,它也会保留相同的区域性。When the UI culture of a thread is set explicitly, that thread retains the same culture even if it crosses application domain boundaries and executes code in another application domain.

隐式设置当前 UI 区域性Implicitly Setting the Current UI Culture

第一次创建线程(包括主应用程序线程)时,默认情况下它的当前 UI 区域性设置如下:When a thread, including the main application thread, is first created, by default its current UI culture is set as follows:

  • 如果属性值不DefaultThreadCurrentUICulture null为,则使用当前应用程序域的属性定义的区域性。By using the culture defined by the DefaultThreadCurrentUICulture property for the current application domain if the property value is not null.

  • 使用系统的默认区域性。By using the system's default culture. 在使用 windows 操作系统的系统上,公共语言运行时调用 Windows GetUserDefaultUILanguage函数来设置当前 UI 区域性。On systems that use the Windows operating system, the common language runtime calls the Windows GetUserDefaultUILanguage function to set the current UI culture. GetUserDefaultUILanguage返回由用户设置的默认 UI 区域性。GetUserDefaultUILanguage returns the default UI culture set by the user. 如果用户未设置默认的 UI 语言,则它将返回最初安装在系统上的区域性。If the user has not set a default UI language, it returns the culture originally installed on the system.

如果线程跨越应用程序边界并在另一个应用程序域中执行代码,则其区域性的确定方式与新创建的线程相同。If the thread crosses application boundaries and executes code in another application domain, its culture is determined in the same way as that of a newly created thread.

请注意,如果您设置的特定 UI 区域性不同于系统安装的 UI 区域性或用户的首选 UI 区域性,并且您的应用程序启动多个线程,则这些线程的当前 UI 区域性将是由GetUserDefaultUILanguage函数,除非在线程执行的应用程序DefaultThreadCurrentUICulture域中将区域性分配给属性。Note that if you set a specific UI culture that is different from the system-installed UI culture or the user's preferred UI culture, and your application starts multiple threads, the current UI culture of those threads will be the culture returned by the GetUserDefaultUILanguage function, unless you assign a culture to the DefaultThreadCurrentUICulture property in the application domain in which the thread is executing.

安全注意事项Security Considerations

更改当前线程的区域性需要SecurityPermission ControlThread具有值集的权限。Changing the culture of the current thread 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.

当前 UI 区域性和 UWP 应用The current UI culture and UWP apps

在通用 Windows 平台(UWP)应用中, CurrentUICulture属性是读写的,就像在 .NET Framework 和 .net Core 应用程序中一样,你可以使用它来获取和设置当前区域性。In Universal Windows Platform (UWP) apps, the CurrentUICulture 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 应用程序中,当前 UI 区域性为每个线程设置, CurrentUICulture属性仅反映当前线程的 UI 区域性。In .NET Framework and .NET Core apps, the current UI culture is a per-thread setting, and the CurrentUICulture property reflects the UI 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
用于设置当前 UI 区域性。for setting the current UI culture. 关联的枚举:ControlThreadAssociated enumeration: ControlThread

适用于

另请参阅