通过


Thread.CurrentCulture 属性

定义

获取或设置当前线程的区域性。

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

属性值

一个对象,表示当前线程的区域性。

例外

属性设置为 null.

仅 .NET Core 和 .NET 5+ :不支持从另一个线程读取或写入线程的区域性。

示例

以下示例演示线程语句,该语句允许 Windows 窗体应用程序的用户界面显示在控制面板中设置的区域性中。 需要其他代码。

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());
    }
}
open System.Threading
open System.Windows.Forms

type UICulture() =
    inherit Form()

    do
        // 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.

new UICulture() |> Application.Run
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 上,如果线程尝试在不同的线程上读取或写入CurrentCulture属性,InvalidOperationException则会引发该属性。 建议使用 CultureInfo.CurrentCulture 属性来检索和设置当前区域性。

从 .NET Framework 4 开始,可以将属性 CurrentCulture 设置为中性区域性。 这是因为类的行为CultureInfo发生了变化:当类表示中性区域性时,其属性值(特别是、特别是Calendar、、CompareInfoDateTimeFormatNumberFormatTextInfo属性)现在反映了与中性区域性关联的特定区域性。 在 .NET Framework 的早期版本中,当分配了中性区域性时,该 CurrentCulture 属性会 NotSupportedException 引发异常。

适用于