ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to demonstrate the use of GetEnvironmentVariable() method of Environment class

Here, we are going to demonstrate the use of SetEnvironmentVariable() method of Environment class in C#.Net.
Submitted by Nidhi, on April 24, 2021

Here, we will learn about the SetEnvironmentVariable() method of the Environment class. This method is used the set value of the environment variable.

Syntax:

void Environment.SetEnvironmentVariable(string var, string val);

Parameter(s):

  • var: Environment variable name
  • val: Value that we store into a variable

Return value:

It does not return any value.

Exception(s):

  • System.Security.SecurityException
  • System.ArgumentNullException
  • System.ArgumentException

Program:

The source code to demonstrate the use of SetEnvironmentVariable() method of Environment class is given below. The given program is compiled and executed successfully.

using System;
using System.IO;

class Sample
{
    //Entry point of Program
    static public void Main()
    {
        string Var_Name = "VAR1";
        string Var_Value = "True";

        if (Environment.GetEnvironmentVariable(Var_Name) == null)
        {
            Environment.SetEnvironmentVariable(Var_Name, Var_Value);
            Console.WriteLine("Value stored in environment variable");
        }
        else
        {
            Console.WriteLine("Value already stored in environment variable");
        }
    }
}

Output:

Value stored in environment variable
Press any key to continue . . .

C# Environment Class Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.