How to check if appSetting exists in web config file

S

softwareakash

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
 
W

Winista

Here is the problem..

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top