Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Dipping into the Environment

I want to dip into the machine in Figure 1 and draw out some useful data. The C# language includes facilities for doing just that. Listing 2 shows some code that uses the System.Environment class.

Listing 2 Drawing some data from the environment.

using System;
using System.IO;

class DumpPlatformData
public void DumpData()
Console.WriteLine("HasShutdownStarted: " +
Console.WriteLine("MachineName: " +
Console.WriteLine("OSVersion: " +

If you run the code in Listing 2, you should see output similar to that shown in Figure 2. In fact, Figure 2 is an abbreviated version of the final program.

Figure 2

Figure 2 Program output.

The first time I ran this code, I found the description of the operating system (OSVersion in Figure 2) quite interesting! This particular machine runs Windows XP, not Windows NT. But the result isn’t wrong—it’s just a matter of the way the operating system sees itself. The Windows XP platform is derived from the Windows NT product line (one reason why it’s a fairly solid operating system). It’s a bit like the difference between a laptop computer and a notebook computer; they’re really just different marketing terms.

You can see from Figure 2 that we’re starting to get a picture of the lone device in Figure 1. We now know the name of the managed system and that it isn’t in the process of shutting down. The System.Environment class has many more methods you can call, as Listing 3 points out.

Listing 3 System.Environment methods.

Console.WriteLine("TickCount: " +
Console.WriteLine("UserDomainName: " +
Console.WriteLine("UserInteractive: " +
Console.WriteLine("UserName: " +
Console.WriteLine("WorkingSet: " +
String query =
"System drive: %SystemDrive% and
system root: %SystemRoot%";
str = Environment.ExpandEnvironmentVariables(query);
Console.WriteLine("ExpandEnvironmentVariables: " +
 nl + " " + str);
Console.WriteLine("Temporary directory: " +
 nl + " " + Environment.GetEnvironmentVariable("TEMP"));
String[] drives = Environment.GetLogicalDrives();
Console.WriteLine("Logical Drives: " +
 String.Join(", ", drives));

Most of the data in Listing 3 is fairly self-explanatory. The TickCount entity refers to the amount of time, in ticks, since the system booted. The UserName is the login name of the current user. What’s important in Listing 3 is that the data can be used to gain a clearer picture of the managed system. This is what I mean by "dipping into" the managed system.

The facilities on the managed system that allow us to dip in like this are often referred to as instrumentation. This term has its origins in managed network devices such as routers. To manage such devices, it’s necessary to have specific instrumentation software on the devices. A management system then can interact with the instrumentation to make sure that it’s running properly, perform upgrades, make backups, etc. Without instrumentation, it’s difficult or impossible to manage entities.

Let’s take a break now and look at the SharpDevelop debugger. This tool is easy to use and helps in tracking down a wide variety of problems.

  • + Share This
  • 🔖 Save To Your Account