CultureInfo.CurrentUICulture 屬性

定義

取得或設定 CultureInfo 物件,此物件代表 Resource Manager 用於執行階段查詢特定文化特性資源的目前使用者介面文化特性。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(); void set(System::Globalization::CultureInfo ^ value); };
public:
 static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentUICulture { get; set; }
public static System.Globalization.CultureInfo CurrentUICulture { get; }
member this.CurrentUICulture : System.Globalization.CultureInfo with get, set
member this.CurrentUICulture : System.Globalization.CultureInfo
Public Shared Property CurrentUICulture As CultureInfo
Public Shared ReadOnly Property CurrentUICulture As CultureInfo

屬性值

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.

範例

下列程式碼範例示範如何變更 CurrentCulture CurrentUICulture 目前線程的和。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 ,設定 CultureInfo 指派給屬性的物件 System.Threading.Thread.CurrentThread.CurrentUICultureThis 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 執行緒執行所在的應用程式域中,抓取屬性所指定的文化特性(如果屬性值不是) nullBy 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 文化特性變更為巴西的葡萄牙文 () 並啟動六個工作,每個工作都會顯示其執行緒識別碼、工作識別碼,以及其目前的 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
    

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

  • 藉由呼叫 Windows 函式 GetUserDefaultUILanguageBy calling the Windows GetUserDefaultUILanguage function.

注意

在中 .NET Compact Framework.NET Compact FrameworkCurrentUICulture 屬性是唯讀的。In 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.

從開始 .NET Framework 4.6.NET Framework 4.6 ,若要變更執行緒所使用的使用者介面文化特性,請將 Thread.CurrentUICulture 屬性設定為新的文化特性。Starting 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.6CultureInfo.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. 藉由抓取屬性的值,您可以取得目前線程的 UI 文化 CultureInfo.CurrentUICulture 特性,如下列範例所示。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]

您也可以從屬性中取出目前線程的 UI 文化 Thread.CurrentUICulture 特性值。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

從開始 .NET Framework 4.6.NET Framework 4.6 ,您可以藉由將 CultureInfo 代表新文化特性的物件指派給屬性,來變更目前的 UI 文化 CultureInfo.CurrentUICulture 特性。Starting 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 或取消刪除) 或非文化特性 (例如 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 代表該文化特性的物件指派給執行緒的屬性,明確地設定任何執行緒的 UI 文化 Thread.CurrentUICulture 特性。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如果屬性值不是,則使用目前應用程式域之屬性所定義的文化特性 nullBy 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 作業系統的系統上,common language runtime 會呼叫 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 文化特性不同,而且您的應用程式會啟動多個執行緒,則 GetUserDefaultUILanguage 除非您將文化特性指派給 DefaultThreadCurrentUICulture 執行執行緒之應用程式域中的屬性,否則這些執行緒目前的 ui 文化特性會是函式所傳回的文化特性。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 具有設定值的許可權 ControlThreadChanging 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. CurrentCultureCurrentUICulture 屬性會對應到 DefaultCoNtext 中的第一個值。 ApplicationModel的集合。The 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 應用程式中,目前的文化特性會對應到 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.

適用於

另請參閱