Have you ever found yourself needing to obtain environment details concerning the server where your application resides? If so, take a look at the System Namespace and specifically the Environment Class which provides information about, and means to manipulate, the current environment and platform. A little later in this article I provide a working example. This class is an extremely useful way to obtain details on the following properties:
- CommandLine – Gets the command line for this process.
- CurrentDirectory – Gets or sets the fully qualified path of the current working directory.
- ExitCode – Gets or sets the exit code of the process.
- HasShutdownStarted – Gets a value indicating whether the common language runtime is shutting down or the current application domain is unloading.
- MachineName – Gets the NetBIOS name of this local computer.
- NewLine – Gets the newline string defined for this environment.
- OSVersion – Gets an OperatingSystem object that contains the current platform identifier and version number.
- ProcessorCount – Gets the number of processors on the current machine.
- StackTrace – Gets current stack trace information.
- SystemDirectory – Gets the fully qualified path of the system directory.
- TickCount – Gets the number of milliseconds elapsed since the system started.
- UserDomainName – Gets the network domain name associated with the current user.
- UserInteractive – Gets a value indicating whether the current process is running in user interactive mode.
- UserName – Gets the user name of the person who is currently logged on to the Windows operating system.
- Version – Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
- WorkingSet – Gets the amount of physical memory mapped to the process context.
The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks is a collection of solutions to the most common ASP.NET problems. Let five world-class ASP.NET professionals guide you through countless practical solutions using C#.
Employing the Environment Class is straight forward. Here is an example:
using System;
using System.Management;
///
/// Summary description for EnvironmentDetails
///
public static class EnvironmentDetails
{
///
/// Name of the machine
///
public static string MachineName
{
get { return System.Environment.MachineName; }
}
///
/// Gets the user name that the application is running under
///
public static string UserName
{
get { return System.Environment.UserName; }
}
///
/// Name of the domain that the application is running under
///
public static string DomainName
{
get { return System.Environment.UserDomainName; }
}
///
/// Name of the operating system
///
public static string OSName
{
get { return System.Environment.OSVersion.Platform.ToString(); }
}
///
/// Version information about the operating system running
///
public static string OSVersion
{
get { return System.Environment.OSVersion.Version.ToString(); }
}
///
/// The service pack running on the operating system
///
public static string OSServicePack
{
get { return System.Environment.OSVersion.ServicePack; }
}
///
/// Full name, includes service pack, version, etc.
///
public static string OSFullName
{
get { return System.Environment.OSVersion.VersionString; }
}
///
/// Fully qualified path of the current working directory
///
public static bool Is64BitOperatingSystem
{
get { return System.Environment.Is64BitOperatingSystem; }
}
///
/// Gets the current stack trace information
///
public static string StackTrace
{
get { return System.Environment.StackTrace; }
}
///
/// Returns the number of processors on the machine
///
public static int NumberOfProcessors
{
get { return System.Environment.ProcessorCount; }
}
}
All you need to do next is write out these properties. In this example I will use a web form and return the result to a Label control.
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<b>Domain Name :</b> " + EnvironmentDetails.DomainName + "<br />");
sb.AppendLine("<b>x64 OS :</b> " + EnvironmentDetails.Is64BitOperatingSystem + "<br />");
sb.AppendLine("<b>Machine Name :</b> " + EnvironmentDetails.MachineName + "<br />");
sb.AppendLine("<b>Number Processors:</b> " + EnvironmentDetails.NumberOfProcessors + "<br />");
sb.AppendLine("<b>Full OS Name :</b> " + EnvironmentDetails.OSFullName + "<br />");
sb.AppendLine("<b>OS Name :</b> " + EnvironmentDetails.OSName + "<br />");
sb.AppendLine("<b>OS Service Pack :</b> " + EnvironmentDetails.OSServicePack + "<br />");
sb.AppendLine("<b>OS Version :</b> " + EnvironmentDetails.OSVersion + "<br />");
sb.AppendLine("<b>Stack Trace :</b> " + EnvironmentDetails.StackTrace + "<br />");
sb.AppendLine("<b>User Name :</b> " + EnvironmentDetails.UserName);
LabelDetails.Text = sb.ToString();
}
Upon executing the web form you are presented the following detail:
As you can see the Environment Class is both powerful and at your disposal.
Related posts:






