C# - Environment.GetEnvironmentVariables() Method with Example

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

Environment.GetEnvironmentVariables() Method

The Environment.GetEnvironmentVariables() is used to get the environment variables.

Syntax

IDictionary Environment.GetEnvironmentVariables();

Parameter(s)

This method returns the object of IDictionary class.

Return Value

This method returns an array of strings that contains the name of logical drives of the computer system.

To use IDictionary class we need to include System.Collections namespace.

Exception(s)

  • System.Security.SecurityException
  • System.OutOfMemoryException

C# Example of Environment.GetEnvironmentVariables() Method

The source code to get the environment variables using Environment class is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class Sample
{
    //Entry point of Program
    static public void Main()
    {
        IDictionary evVars = Environment.GetEnvironmentVariables();
        foreach (DictionaryEntry eVar in evVars)
        {
            Console.WriteLine("{0}:{1}", eVar.Key, eVar.Value);
        }
    }
}

Output

Path:C:\Program Files\Dell\DW WLAN Card;;;C:\Windows\system32;C:\Windows;C:\Wind
ows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\M
icrosoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DT
S\Binn\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\Skype\Phon
e\
TEMP:C:\Users\Arvind\AppData\Local\Temp
SESSIONNAME:Console
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PSModulePath:C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
USERDOMAIN:Arvind-PC
PROCESSOR_ARCHITECTURE:x86
SystemDrive:C:
VS100COMNTOOLS:C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\
APPDATA:C:\Users\Arvind\AppData\Roaming
windir:C:\Windows
LOCALAPPDATA:C:\Users\Arvind\AppData\Local
TMP:C:\Users\Arvind\AppData\Local\Temp
USERPROFILE:C:\Users\Arvind
ProgramFiles:C:\Program Files
FP_NO_HOST_CHECK:NO
HOMEPATH:\Users\Arvind
COMPUTERNAME:ARVIND-PC
USERNAME:Arvind
NUMBER_OF_PROCESSORS:4
PROCESSOR_IDENTIFIER:x86 Family 6 Model 42 Stepping 7, GenuineIntel
VBOX_INSTALL_PATH:C:\Program Files\Oracle\VirtualBox\
SystemRoot:C:\Windows
ComSpec:C:\Windows\system32\cmd.exe
LOGONSERVER:\\ARVIND-PC
VisualStudioDir:C:\Users\Arvind\Documents\Visual Studio 2010
CommonProgramFiles:C:\Program Files\Common Files
PROCESSOR_LEVEL:6
PROCESSOR_REVISION:2a07
PROMPT:$P$G
ALLUSERSPROFILE:C:\ProgramData
PUBLIC:C:\Users\Public
ProgramData:C:\ProgramData
OS:Windows_NT
HOMEDRIVE:C:
Press any key to continue . . .

C# Environment Class Programs »





Comments and Discussions!

Load comments ↻





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