Here is my code to check whether environment variable is defined or not
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
if(Environment.GetEnvironmentVariable("qwert")==null)
Console.WriteLine(Environment.GetEnvironmentVariable("qwert"));
Console.WriteLine("hello");
}
}
}
But the problem is if I set my environment variable value as null it is executing the if statement. What is the workaround for this? The code should work for both the conditions any variable value is set or it is set as null.
Your code should be
if(!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("qwert")))
Console.WriteLine(Environment.GetEnvironmentVariable("qwert"));
Console.WriteLine("hello");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With