×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

C# - Environment.SetEnvironmentVariable() Method with Example

In this tutorial, we will learn about the C# Environment.SetEnvironmentVariable() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 30, 2023

Environment.SetEnvironmentVariable() Method

The Environment.SetEnvironmentVariable() 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

C# Example of Environment.SetEnvironmentVariable() method

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.