---
title: "Environment Class in C#"  
description: "In this blog I am trying to explain the concept of Environment Class in C#.Environment ClassEnvironment class use to retrieve information such as comm"  
author: "Vijay Shukla"  
published: 2013-06-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/524/environment-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Environment Class in C#

In this blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of [Environment](https://yourviews.mindstick.com/view/308/need-to-create-a-supportive-work-environment-for-women) [Class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example).

##### Environment Class

Environment class use to retrieve information such as command-[line arguments](https://www.mindstick.com/forum/158085/how-do-i-pass-command-line-arguments-to-a-node-js-program), the [exit code](https://www.mindstick.com/forum/33416/can-t-start-eclipse-java-was-started-but-returned-exit-code-13), environment [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) settings, contents of the call stack, time since last system boot, and the version of the [common language runtime](https://www.mindstick.com/articles/16/common-language-runtime). Environment class provides information about, and means to manipulate, the current environment and platform. Environment class cannot be inherited.

##### Syntax:-

```
 [ComVisibleAttribute(true)] public static class Environmentusing System;using System.Collections;
class Sample{
    public static void Main()    {
        String str;        String nl = Environment.NewLine;
        //        Console.WriteLine();        Console.WriteLine("-- Environment members --");        // Invoke this sample with an arbitrary set of command line arguments.        Console.WriteLine("CommandLine: {0}", Environment.CommandLine);        String[] arguments = Environment.GetCommandLineArgs();        Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
        // <-- Keep this information secure! -->        Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);
        Console.WriteLine("ExitCode: {0}", Environment.ExitCode);        Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);
        // <-- Keep this information secure! -->        Console.WriteLine("MachineName: {0}", Environment.MachineName);
        Console.WriteLine("NewLine: {0} first line{0} second line{0} third line",
                              Environment.NewLine);
        Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
        Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
        // <-- Keep this information secure! -->
        Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);
        Console.WriteLine("TickCount: {0}", Environment.TickCount);
       // <-- Keep this information secure! -->
        Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
        Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);
        // <-- Keep this information secure! -->
        Console.WriteLine("UserName: {0}", Environment.UserName);
        Console.WriteLine("Version: {0}", Environment.Version.ToString());
        Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);
        // No example for Exit(exitCode) because doing so would terminate this example.
       // <-- Keep this information secure! -->
        String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
        str = Environment.ExpandEnvironmentVariables(query);
        Console.WriteLine("ExpandEnvironmentVariables: {0} {1}", nl, str);
        Console.WriteLine("GetEnvironmentVariable: {0} My temporary directory is {1}.", nl,
                               Environment.GetEnvironmentVariable("TEMP"));
        Console.WriteLine("GetEnvironmentVariables: ");
        IDictionary environmentVariables = Environment.GetEnvironmentVariables();
        foreach (DictionaryEntry de in environmentVariables)
        {            Console.WriteLine(" {0} = {1}", de.Key, de.Value);
        }
        Console.WriteLine("GetFolderPath: {0}",                     Environment.GetFolderPath(Environment.SpecialFolder.System));
        String[] drives = Environment.GetLogicalDrives();        Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
        Console.ReadKey();    }
}
```

**Note**:- Above line of code displays a list of information about the current system environment.

##### Output:-

![Environment Class in C#](https://www.mindstick.com/blogs/377d0fde-033a-4e60-85c2-1fb85b6d9563/images/3314322a-e7f7-4926-8e80-fb859753de42.png)

---

Original Source: https://www.mindstick.com/blog/524/environment-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
