ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to get the environment variables using Environment class

Here, we are going to learn how to get the environment variables using Environment class in C#.Net?
Submitted by Nidhi, on April 23, 2021

To get environment variables using the Environment class – we use the GetEnvironmentVariables() method of the Environment class.

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

Program:

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 »

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.