C# .Net what’s in the Environment class?

Thought this would be interesting to show what’s in the System.Environment class, the code below is just borrowed from the MSDN documentation with a few tidy ups. It’s useful to know what the Environment class provides.

using System;
using System.Collections;

namespace CsharpEnvironmentDemo
{
    class CsharpEnvironmentExample
    {
        static void Main()
        {
            var newLine = Environment.NewLine;
            Console.WriteLine();

            Console.WriteLine("Output from the Environment class:");
            Console.WriteLine();

            Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
            Console.WriteLine();

            var arguments = Environment.GetCommandLineArgs();
            Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
            Console.WriteLine();

            Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);
            Console.WriteLine();

            Console.WriteLine("ExitCode: {0}", Environment.ExitCode);
            Console.WriteLine();

            Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);
            Console.WriteLine();

            Console.WriteLine("MachineName: {0}", Environment.MachineName);
            Console.WriteLine();

            Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line",
                                  Environment.NewLine);
            Console.WriteLine();

            Console.WriteLine("OSVersion: {0}", Environment.OSVersion);
            Console.WriteLine();

            Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
            Console.WriteLine();

            Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);
            Console.WriteLine();

            Console.WriteLine("TickCount: {0}", Environment.TickCount);
            Console.WriteLine();

            Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
            Console.WriteLine();

            Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);
            Console.WriteLine();

            Console.WriteLine("UserName: {0}", Environment.UserName);
            Console.WriteLine();

            Console.WriteLine("Version: {0}", Environment.Version);
            Console.WriteLine();

            Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);
            Console.WriteLine();

            const string query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
            var str = Environment.ExpandEnvironmentVariables(query);

            Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", newLine, str);
            Console.WriteLine();

            Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", newLine,
                                   Environment.GetEnvironmentVariable("TEMP"));
            Console.WriteLine();

            Console.WriteLine("GetEnvironmentVariables: ");
            var environmentVariables = Environment.GetEnvironmentVariables();
            foreach (DictionaryEntry entry in environmentVariables)
                Console.WriteLine("  {0} = {1}", entry.Key, entry.Value);

            Console.WriteLine();

            Console.WriteLine("GetFolderPath: {0}",
                         Environment.GetFolderPath(Environment.SpecialFolder.System));
            Console.WriteLine();

            var drives = Environment.GetLogicalDrives();
            Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));

            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *