简体   繁体   中英

How to read programmatically web.config values?

I use asp.net and c# 4. I have a Web.Config file

<globalization culture="auto:fr" uiCulture="fr"/>

I want to get this value in a new variable programmatically in Code Behind.

var test = .......

How to get value for culture?

Solution Thanks to your Answers:

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
GlobalizationSection section = (GlobalizationSection)config.GetSection("system.web/globalization");

OpenWebConfiguration("/"); // Point to Physical path for the Web.Config file (Useful when using Routing).

GetSection("system.web/globalization"); // Get the globalization section within the system.web node.

It's a GlobalizationSection , so you can get at it via

var globalizationSection = 
        WebConfigurationManager.GetSection("globalization") as GlobalizationSection;

You might need to import the System.Configuration and System.Web.Configuration namespaces to do this, but you can do something like this:

//and here is the code to get the section
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");     
GlobalizationSection section = config.GetSection("globalization") as GlobalizationSection;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM