By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

How to check if appSetting exists in web config file

P: n/a
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

Jul 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Here is the problem..

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

"softwareakash" <so***********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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

Jul 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.