Thread.CurrentCulture 属性

定义

获取或设置当前线程的区域性。Gets or sets the culture for the current thread.

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

属性值

表示当前线程的区域性的对象。An object that represents the culture for the current thread.

异常

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

仅限 .NET Core:不支持从一个线程读取或写入另一个线程的区域性。.NET Core only: Reading or writing the culture of a thread from another thread is not supported.

示例

下面的示例演示了线程语句,该语句允许 Windows 窗体应用程序的用户界面显示在控制面板中设置的区域性。The following example shows the threading statement that allows the user interface of a Windows Forms application to display in the culture that is set in Control Panel. 需要其他代码。Additional code is needed.

#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 对象及其关联的对象,确定日期、时间、数字、货币值、文本的排序顺序、大小写约定和字符串比较的默认格式。The CultureInfo object that is returned by this property, together with its associated objects, determine the default format for dates, times, numbers, currency values, the sorting order of text, casing conventions, and string comparisons. 请参阅 CultureInfo 类,了解区域性名称和标识符、固定、非特定区域性和特定区域性之间的差异,以及区域性信息影响线程和应用程序域的方式。See the CultureInfo class to learn about culture names and identifiers, the differences between invariant, neutral, and specific cultures, and the way culture information affects threads and application domains. 请参阅 CultureInfo.CurrentCulture 属性,了解如何确定如何确定线程的默认区域性,以及用户如何为其计算机设置区域性信息。See the CultureInfo.CurrentCulture property to learn how a thread's default culture is determined, and how users set culture information for their computers.

重要

与当前线程以外的任何线程一起使用时,CurrentCulture 属性不能可靠地工作。The CurrentCulture property doesn't work reliably when used with any thread other than the current thread. 在 .NET Framework 中,读取属性是可靠的,但对于除当前线程之外的其他线程,其设置是可靠的。In .NET Framework, reading the property is reliable, although setting it for a thread other than the current thread is not. 在 .NET Core 中,如果某个线程尝试在另一个线程上读取或写入 CurrentCulture 属性,则会引发 InvalidOperationExceptionOn .NET Core, an InvalidOperationException is thrown if a thread attempts to read or write the CurrentCulture property on a different thread. 建议使用 CultureInfo.CurrentCulture 属性检索和设置当前区域性。We recommend that you use the CultureInfo.CurrentCulture property to retrieve and set the current culture.

.NET Framework 4.NET Framework 4开始,可以将 CurrentCulture 属性设置为非特定区域性。Beginning with the .NET Framework 4.NET Framework 4, you can set the CurrentCulture property to a neutral culture. 这是因为 CultureInfo 类的行为已更改:当它表示非特定区域性时,其属性值(特别是 CalendarCompareInfoDateTimeFormatNumberFormatTextInfo 属性)现在反映了与非特定区域性关联。This is because the behavior of the CultureInfo class has changed: When it represents a neutral culture, its property values (in particular, the Calendar, CompareInfo, DateTimeFormat, NumberFormat, and TextInfo properties) now reflect the specific culture that is associated with the neutral culture. 在 .NET Framework 的早期版本中,在分配非特定区域性时,CurrentCulture 属性将引发 NotSupportedException 异常。In earlier versions of the .NET Framework, the CurrentCulture property threw a NotSupportedException exception when a neutral culture was assigned.

安全性

SecurityPermission
用于设置属性。for setting the property. 关联的枚举: ControlThreadAssociated enumeration: ControlThread

适用于