Open In App
Related Articles

C# Program to Show the Use of GetEnvironmentVariable() Method of Environment Class

Improve Article
Save Article
Like Article

In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. By just using some predefined methods we can get the information of the Operating System using the Environment class and GetEnvironmentVariable() method is one of them. This method is used to get the value of an environment variable from the current process.



where directory_name is the directory

Return: This method will return the string that will represent the environment variable or it will return null if the environment variable is not found.

Exceptions: This method will throw the following exceptions:

  • ArgumentNullException is thrown when the variable is null.
  • SecurityException is thrown when the caller does not have the required permission to perform this operation.



// C# program to illustrate the GetEnvironmentVariable()
// method of Environment Class
using System;
using System.IO;
class GFG{
static public void Main()
    // Get the windows directory
    // environment details
    Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
    DirectoryInfo info = new DirectoryInfo(".");
    // Display the information
    Console.WriteLine("Information:\n" + info.FullName);


Last Updated : 16 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials