Thread.CurrentCulture 屬性

定義

取得或設定目前執行緒的文化特性 (Culture)。

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

屬性值

CultureInfo

表示目前執行緒之文化特性的物件。

例外狀況

該屬性設定為 null

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

範例

下列範例顯示的執行緒語句可讓 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

備註

CultureInfo這個屬性所傳回的物件與其相關聯的物件,會決定日期、時間、數位、貨幣值、文字排序次序、大小寫慣例及字串比較的預設格式。 請參閱 CultureInfo 類別以瞭解文化特性名稱和識別碼、非變異、中性和特定文化特性之間的差異,以及文化特性資訊影響執行緒和應用程式域的方式。 請參閱 CultureInfo.CurrentCulture 屬性,瞭解如何判斷線程的預設文化特性,以及使用者如何設定其電腦的文化特性資訊。

重要

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

從 .NET Framework 4 開始,您可以將屬性設定 CurrentCulture 為中性文化特性。 這是因為類別的行為 CultureInfo 已變更:當它代表中立的文化特性時,其屬性值 (特別是、、 CalendarCompareInfo DateTimeFormat NumberFormatTextInfo 屬性,) 現在會反映與中性文化特性相關聯的特定文化特性。 在舊版的 .NET Framework 中, CurrentCulture NotSupportedException 當指派了中立文化特性時,屬性會擲回例外狀況。

適用於