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.
Syntax:
Environment.GetEnvironmentVariable(“directory_name”)
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.
Example:
C#
using System;
using System.IO;
class GFG{
static public void Main()
{
Environment.CurrentDirectory = Environment.GetEnvironmentVariable( "windir" );
DirectoryInfo info = new DirectoryInfo( "." );
Console.WriteLine( "Information:\n" + info.FullName);
}
}
|
Output:
Information:
D:\Windows