107

My XML looks like this and the filename is web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>   
    <add key="configFile" value="IIS.config"/>
    <add key="RialtoDomain" value="ASNC_AUDITORS"/>    
  </appSettings>
  <system.serviceModel>
    ....
  </system.serviceModel>
</configuration>

In the code when I read like this

String path = ConfigurationSettings.AppSettings["configFile"];

I am getting a null value. No exception is thrown. Is this the right way to do it?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
realn
  • 1,732
  • 3
  • 12
  • 20
  • You can also read web.config appSettings values directly from an .aspx page https://stackoverflow.com/questions/4152842/how-to-access-web-config-settings-directly-in-aspx-page – Matthew Lock Nov 25 '21 at 07:00

6 Answers6

168

Since you're accessing a web.config you should probably use

using System.Web.Configuration;

WebConfigurationManager.AppSettings["configFile"]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I have deployed a .net 6 webapi on iis. Should I use app settings file or web config file for settings? – variable Jan 11 '22 at 15:35
33

Add namespace

using System.Configuration;

and in place of

ConfigurationSettings.AppSettings

you should use

ConfigurationManager.AppSettings

String path = ConfigurationManager.AppSettings["configFile"];
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
7
ConfigurationManager.AppSettings["configFile"]

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bassetassen
  • 20,852
  • 8
  • 39
  • 40
  • I checked that still i am getting a null value. I checked the spelling of the key and everything. – realn Nov 16 '11 at 05:53
4

You should add System.configuration dll as reference and use System.Configuration.ConfigurationManager.AppSettings["configFile"].ToString

Don't forget to add usingstatement at the beginning. Hope it will help.

curiousBoy
  • 6,334
  • 5
  • 48
  • 56
0
    using System.Configuration;

    /// <summary>
    /// For read one setting
    /// </summary>
    /// <param name="key">Key correspondent a your setting</param>
    /// <returns>Return the String contains the value to setting</returns>
    public string ReadSetting(string key)
    {
        var appSettings = ConfigurationManager.AppSettings;
        return appSettings[key] ?? string.Empty;
    }

    /// <summary>
    /// Read all settings for output Dictionary<string,string> 
    /// </summary>        
    /// <returns>Return the Dictionary<string,string> contains all settings</returns>
    public Dictionary<string, string> ReadAllSettings()
    {
        var result = new Dictionary<string, string>();
        foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            result.Add(key, ConfigurationManager.AppSettings[key]);
        return result;
    }
williambarau
  • 491
  • 5
  • 4
0

Here's the easy way to get access to the web.config settings anywhere in your C# project.

 Properties.Settings.Default

Use case:

litBodyText.Text = Properties.Settings.Default.BodyText;
litFootText.Text = Properties.Settings.Default.FooterText;
litHeadText.Text = Properties.Settings.Default.HeaderText;

Web.config file:

  <applicationSettings>
    <myWebSite.Properties.Settings> 
      <setting name="BodyText" serializeAs="String">
        <value>
          &lt;h1&gt;Hello World&lt;/h1&gt;
          &lt;p&gt;
      Ipsum Lorem
          &lt;/p&gt;
        </value>
      </setting>
      <setting name="HeaderText" serializeAs="String">
      My header text
        <value />
      </setting>
      <setting name="FooterText" serializeAs="String">
      My footer text
        <value />
      </setting>
    </myWebSite.Properties.Settings>
  </applicationSettings>

No need for special routines - everything is right there already. I'm surprised that no one has this answer for the best way to read settings from your web.config file.

MC9000
  • 2,076
  • 7
  • 45
  • 80