Archive

Posts Tagged ‘web.config’

Read/Write Section from/into Web.config in Asp.net

Many times, we need to read sections from web.config or write into web.config.

I have a requirement where i need to write an section in web.config that points to another config file.

Below is the code where “abc” is the section

Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(“~”);
ConfigurationSection section = objConfig.Sections[“abc”];
string str = “abcdef”;
section.SectionInformation.SetRawXml(str);
objConfig.Save();

The above code will read “abc” section and write the content in str.

For Reading Appsettings from web.config, below is the code

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection appSettings = (AppSettingsSection) config.GetSection(appSettings);

string[] appKeys = appSettings.Settings.AllKeys;

for (int i = 0; i < appSettings.Settings.Count; i++)

{

Response.Write(appSettings.Settings[appKeys[i]].Value);

Response.Write(“<BR>”);

}

Read/Acess globalization section of system.web from web.config

May 30, 2009 2 comments

To read web.config, and find any section there are number of solutions available, one such method to access globalization section which is under system.web of web.config is as follows:

In web.config i have

<globalization uiCulture=”nl-NL”   />

Now to read the uiculture in codebehind file in asp.net

string uicul = ((System.Web.Configuration.GlobalizationSection)(System.Configuration.ConfigurationSettings.GetConfig(“system.web/globalization”))).UICulture;

Lets say for example i have to display date  according to culture set in web.confg so:

public object GetDateCulture(object date)
{
string uicul = ((System.Web.Configuration.GlobalizationSection)(System.Configuration.ConfigurationSettings.GetConfig(“system.web/globalization”))).UICulture;
System.IFormatProvider format = new System.Globalization.CultureInfo(uicul, true);
date = Convert.ToDateTime(date).ToString(“dd MMM yyyy”, format);
return date;
}

Ouput will be:

if culture is english : 22 May 2009

if culture is dutch : 22 Mai 2009