共用方式為


Thread.CurrentUICulture 屬性

定義

取得或設定資源管理員目前用以在執行階段查詢特定文化特性資源所用的文化特性。

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

屬性值

CultureInfo

表示目前文化特性的物件。

例外狀況

該屬性設定為 null

此屬性設為無法用來尋找資源檔的文化特性名稱。 資源檔名稱只可以包含字母、數字、連字號或底線。

僅限 .NET Core 和 .NET 5 +:不支援從另一個執行緒讀取或寫入執行緒的文化特性。

範例

下列範例會判斷目前線程之 UI 文化特性的語言是否為法文。 如果不是,則會將目前線程的 UI 文化特性設定為英文 (美國) 。

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

public class Example
{
   public static void Main()
   {
      // Change the current culture if the language is not French.
      CultureInfo current = Thread.CurrentThread.CurrentUICulture;
      if (current.TwoLetterISOLanguageName != "fr") {
         CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
         Thread.CurrentThread.CurrentUICulture = newCulture;
         // Make current UI culture consistent with current culture.
         Thread.CurrentThread.CurrentCulture = newCulture;
      }
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name);
      Console.WriteLine("The current culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name);
   }
}
// The example displays the following output:
//     The current UI culture is English (United States) [en-US]
//     The current culture is English (United States) [en-US]
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Change the current culture if the language is not French.
      Dim current As CultureInfo = Thread.CurrentThread.CurrentUICulture
      If current.TwoLetterISOLanguageName <> "fr" Then
         Dim newCulture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
         Thread.CurrentThread.CurrentUICulture = newCulture
         ' Make current UI culture consistent with current culture.
         Thread.CurrentThread.CurrentCulture = newCulture
      End If
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name)
      Console.WriteLine("The current culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name)
   End Sub
End Module
' The example displays output like the following:
'     The current UI culture is English (United States) [en-US]
'     The current culture is English (United States) [en-US]

下列程式碼範例會顯示執行緒語句,允許 Windows Forms 的使用者介面顯示在主控台中設定的文化特性。 需要額外的程式碼。

#using <system.dll>
#using <System.Drawing.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Windows::Forms;
ref class UICulture: public Form
{
public:
   UICulture()
   {
      
      // Set the user interface to display in the
      // same culture as that set in Control Panel.
      Thread::CurrentThread->CurrentUICulture = Thread::CurrentThread->CurrentCulture;
      
      // Add additional code.
   }
};


int main()
{
   Application::Run( gcnew UICulture );
}
using System;
using System.Threading;
using System.Windows.Forms;

class UICulture : Form
{
    public UICulture()
    {
        // Set the user interface to display in the
        // same culture as that set in Control Panel.
        Thread.CurrentThread.CurrentUICulture = 
            Thread.CurrentThread.CurrentCulture;

        // Add additional code.
    }

    static void Main()
    {
        Application.Run(new UICulture());
    }
}
Imports System.Threading
Imports System.Windows.Forms

Public Class UICulture : Inherits Form
    Sub New()

        ' Set the user interface to display in the
        ' same culture as that set in Control Panel.
        Thread.CurrentThread.CurrentUICulture = _
            Thread.CurrentThread.CurrentCulture

        ' Add additional code.
    End Sub

    Shared Sub Main()
        Application.Run(New UICulture())
    End Sub
End Class

備註

UI 文化特性會指定應用程式支援使用者輸入和輸出所需的資源,且預設與作業系統文化特性相同。 請參閱 CultureInfo 類別以瞭解文化特性名稱和識別碼、非變異、中性和特定文化特性之間的差異,以及文化特性資訊影響執行緒和應用程式域的方式。 CultureInfo.CurrentUICulture若要瞭解如何判斷線程的預設 UI 文化特性,請參閱屬性。

重要

CurrentUICulture當與目前線程以外的任何執行緒搭配使用時,屬性無法可靠地運作。 在 .NET Framework 中,讀取屬性是可靠的,但針對目前線程以外的執行緒進行設定並不是如此。 在 .NET Core 上, InvalidOperationException 如果執行緒嘗試讀取或寫入不同執行緒上的屬性,就會擲回 CurrentUICulture 。 建議您使用 CultureInfo.CurrentUICulture 屬性來取得和設定目前的文化特性。

CultureInfo這個屬性所傳回的可以是中性文化特性。 中性文化特性不應搭配格式化方法(例如 String.Format(IFormatProvider, String, Object[])DateTime.ToString(String, IFormatProvider) 和)使用 Convert.ToString(Char, IFormatProvider) 。 您 CultureInfo.CreateSpecificCulture 可以使用方法來取得特定文化特性,或是使用 CurrentCulture 屬性。

適用於