如何设置CultureInfo。来自App.Config文件的CurrentCulture ?

时间:2021-10-05 07:28:59

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.

我需要通过App.Config文件设置应用程序的区域性,这样就可以自动使用“pt-BR”解析日期,而无需为每个操作手工通知区域性。

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.

据我所知,有一个全球化部分可以在系统中定义。web中的web部分。配置文件,但是我正在运行一个控制台应用程序,我不能算出来。

Any idea?

任何想法?

3 个解决方案

#1


21  

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

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

<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;

#2


1  

Starting form .Net 4.5 it's possible to set the default thread culture so there is no need to fix it per thread:

从。net 4.5开始,可以设置默认的线程文化,因此不需要对每个线程进行修改:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR");
CultureInfo.DefaultThreadUICurrentCulture = new CultureInfo("pt-BR");

Have not yet found any way that matches web.config globalization section unfortunately.

还没有找到任何与web匹配的方法。不幸的是全球化配置部分。

#3


-3  

using System.Threading;

使用System.Threading;

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("bn-BD");

Thread.CurrentThread。CurrentCulture = new System.Globalization.CultureInfo(“bn-BD”);

//For Bangladesh. I use this line on every page form load event

/ /为孟加拉国。我在每个页面窗体加载事件中都使用这一行

#1


21  

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

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

<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;

#2


1  

Starting form .Net 4.5 it's possible to set the default thread culture so there is no need to fix it per thread:

从。net 4.5开始,可以设置默认的线程文化,因此不需要对每个线程进行修改:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR");
CultureInfo.DefaultThreadUICurrentCulture = new CultureInfo("pt-BR");

Have not yet found any way that matches web.config globalization section unfortunately.

还没有找到任何与web匹配的方法。不幸的是全球化配置部分。

#3


-3  

using System.Threading;

使用System.Threading;

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("bn-BD");

Thread.CurrentThread。CurrentCulture = new System.Globalization.CultureInfo(“bn-BD”);

//For Bangladesh. I use this line on every page form load event

/ /为孟加拉国。我在每个页面窗体加载事件中都使用这一行