如何从 App.Config 文件设置 CultureInfo.CurrentCulture? [英] How do I set CultureInfo.CurrentCulture from an App.Config file?

查看:45
本文介绍了如何从 App.Config 文件设置 CultureInfo.CurrentCulture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 App.Config 文件设置我的应用程序的文化,以便pt-BR"自动用于解析日期,而无需为每个操作手动通知文化.

据我所知,可以在 Web.Config 文件的 system.web 部分内定义一个 globalization 部分,但我正在运行控制台应用程序,我无法弄清楚.

有什么想法吗?

解决方案

我不知道从 App.config 设置它的内置方法,但您可以像这样在 App.config 中定义一个键

<预><代码><配置><应用设置><add key="DefaultCulture" value="pt-BR"/></appSettings></配置>

并在您的应用程序中读取该值并设置区域性

CultureInfoculture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]);Thread.CurrentThread.CurrentCulture = 文化;Thread.CurrentThread.CurrentUICulture = 文化;

此外,正如@Ilya 所提到的,从 .NET 4.5 开始,您可以设置默认文化一次,而不是每个线程:

CultureInfo.DefaultThreadCurrentCulture = 文化CultureInfo.DefaultThreadCurrentUICulture = 文化

I need to set my application's culture through an App.Config file, so that "pt-BR" is used automatically for parsing dates without the need to manually inform the culture for each operation.

As far as I know, there's a globalization section that can be defined inside the system.web section in a Web.Config file, but I'm running a console application and I can't figure this out.

Any idea?

解决方案

I don't know a built-in way to set it from App.config, but you could just define a key in your App.config like this

<configuration>
    <appSettings>
        <add key="DefaultCulture" value="pt-BR" />
    </appSettings>
</configuration>

and in your application read that value and set the culture

CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

Also, as @Ilya has mentioned, since .NET 4.5 you can set the default culture once, rather than per-thread:

CultureInfo.DefaultThreadCurrentCulture = culture
CultureInfo.DefaultThreadCurrentUICulture = culture

这篇关于如何从 App.Config 文件设置 CultureInfo.CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆