How to check if appSetting exists in web config file

Discussion in 'ASP .Net' started by softwareakash, Jul 27, 2006.

  1. Hi

    I have a class library which takes some values from web config / app
    config files when called.
    I am initialising these values when my object gets called.

    Is there any method to find out if app Settings tag is defined in the
    file?

    here is my code for initislisation

    public class WriteFile
    {
    // Initialising variables from the config Files
    private static readonly string FullFilePath =
    GetFilePathConfigSetting("FilePath");

    and here is the code for GetFilePathConfigSetting which checks for the
    value

    public static string GetFilePathConfigSetting(string name)
    {

    string retVal = ConfigurationSettings.AppSettings[name];

    if (retVal == null)
    {

    return "defaultfile.txt";
    }

    return retVal;

    }

    The problem is that when I deliberately remove appSettings tag or the
    Filepath tag from the web config.. there is an exception.

    can anybody tell where I am making a mistake?

    Thanks in advance
    Akash
    softwareakash, Jul 27, 2006
    #1
    1. Advertising

  2. softwareakash

    Winista Guest

    Here is the problem..

    if (ConfigurationSettings.AppSettings[name] != null)
    {
    // Now do your magic..
    }

    "softwareakash" <> wrote in message
    news:...
    > Hi
    >
    > I have a class library which takes some values from web config / app
    > config files when called.
    > I am initialising these values when my object gets called.
    >
    > Is there any method to find out if app Settings tag is defined in the
    > file?
    >
    > here is my code for initislisation
    >
    > public class WriteFile
    > {
    > // Initialising variables from the config Files
    > private static readonly string FullFilePath =
    > GetFilePathConfigSetting("FilePath");
    >
    > and here is the code for GetFilePathConfigSetting which checks for the
    > value
    >
    > public static string GetFilePathConfigSetting(string name)
    > {
    >
    > string retVal = ConfigurationSettings.AppSettings[name];
    >
    > if (retVal == null)
    > {
    >
    > return "defaultfile.txt";
    > }
    >
    > return retVal;
    >
    > }
    >
    > The problem is that when I deliberately remove appSettings tag or the
    > Filepath tag from the web config.. there is an exception.
    >
    > can anybody tell where I am making a mistake?
    >
    > Thanks in advance
    > Akash
    >
    Winista, Jul 27, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maziar Aflatoun

    Web.config reading the appSetting

    Maziar Aflatoun, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,301
    George Durzi
    Nov 14, 2003
  2. Amy

    appSetting printing on page

    Amy, Oct 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    364
  3. Gaspar
    Replies:
    0
    Views:
    324
    Gaspar
    Aug 22, 2008
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,118
  5. Thomas

    appsetting read write

    Thomas, Dec 17, 2005, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    112
    Thomas
    Dec 17, 2005
Loading...

Share This Page