At the startup of my application, I need to ensure that the user settings are complete before the user is allowed to select a "Run" button to start the functional thread. I have an Init.cs class which runs various checks to make sure the environment is set up correctly to prevent common errors to occur on running. Most of these checks work fine, except for the following:
static void checkConfigHealth()
{
Console.WriteLine("Checking Config");
Settings.Default["nullVal"] = false;
Console.WriteLine(Settings.Default["Email"]); //Test code
if (Settings.Default["Email"] == null)
{
Settings.Default["nullVal"] = true;
}
if (Settings.Default["SQL"] == null)
{
Settings.Default["nullVal"] = true;
}
if (Settings.Default["Server"] == null)
{
Settings.Default["nullVal"] = true;
}
if (Settings.Default["User"] == null)
{
Settings.Default["nullVal"] = true;
}
bool nullVal = Convert.ToBoolean(Settings.Default["nullVal"]);
if (nullVal)
{
Console.WriteLine("Please update the configuartion!");
}
Console.WriteLine(Settings.Default["nullVal"]); //Test code
}
It goes on but that's the basic idea. When this method is called at the startup, I get responses from the test code (assuming that the settings.default values are empty):
"Checking Config"
//I get a blank here where an 'email' entry would go (this is correct, because I have left it blank on purpose for testing)
False
//Saying that the bool nullVal is false (this should say true)
My settings are emtpy. According to my test responses, the method is called correctly and the settings.default values are empty, but it does not trigger:
if(Settings.Default["Email"] == null)
If I updated the config settings and enter nothing after the application has started, then hit my Run button, these checks are run again, and they work fine. They just don't work at startup. It acts as if there is something there (I'm assuming '/0'?)
I found suggestions on stackoverflow saying to do exactly what I'm doing. Is there another way to check if settings are empty? Sorry if my question is confusing.
The default value for string in the Settings is empty string and not null. Try to change the condition to the following:
if(string.IsNullOrEmpty(Settings.Default["Email"])
instead of:
if(Settings.Default["Email"] == null)