typestar

Environment in C#

What the process knows about where it is running.

Console.WriteLine(Environment.MachineName);
Console.WriteLine(Environment.ProcessorCount);
Console.WriteLine(Environment.CurrentDirectory);

// environment variables, with a shrug for the missing ones
var home = Environment.GetEnvironmentVariable("HOME") ?? "(not set)";
Console.WriteLine(home);

var profile = Environment.GetFolderPath(
    Environment.SpecialFolder.UserProfile);
Console.WriteLine(profile);

How it works

  1. MachineName, ProcessorCount and the current directory, direct.
  2. GetEnvironmentVariable returns null; ?? supplies the shrug.
  3. GetFolderPath resolves well-known folders portably.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
405
Tokens
68
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 43 seconds.

Type this snippet

Step 2 of 3 in Diagnostics & environment, step 14 of 17 in The .NET library.

← Previous Next →