Working with the environment in .NET
Takeaway: It is often important to know the details of the environment in which an application is running. The System.Environment class makes it easy to get these values. Here's how.
While .NET applications usually reside on a Windows-based machine (Mono provides non-Windows support), the version and other system characters will vary. For instance, you may need to access the current machine's name, folder locations, operating system version, current user name, and so forth. The .NET Framework provides the answers within the Environment class located in the System namespace.
Inside the class
The System.Environment class allows you to retrieve information about the host environment. This includes the following:
You can access these pieces of information via properties and methods contained within the class. Let's look at them before diving into code examples. We begin with the methods available in the System.Environment class:
These properties and methods are easy to utilize in your applications. Let's examine them in action with a few examples.
Operating system version
You may find yourself in situations where knowing the current operating system is important. The next example shows how you may determine it with a breakdown of the value(s) returned.
using System;
namespace BuilderExamples {
class Class1 {
[STAThread]
static void Main(string[] args) {
Version v;
Console.WriteLine("The current user is: " +
Environment.UserName);
v = Environment.Version;
Console.WriteLine("Build: " + v.Build.ToString());
Console.WriteLine("Master: " + v.Major.ToString());
Console.WriteLine("Minor: " + v.Minor.ToString());
Console.WriteLine("Revision: " + v.Revision.ToString());
} } }
The VB.NET version follows:
Imports System
Module Module1
Sub Main()
Dim v As Version
Console.WriteLine("The current user is: " +
Environment.UserName)
v = Environment.Version
Console.WriteLine("Build: " + v.Build.ToString())
Console.WriteLine("Master: " + v.Major.ToString())
Console.WriteLine("Minor: " + v.Minor.ToString())
Console.WriteLine("Revision: " + v.Revision.ToString())
End Sub
The values returned will depend upon your system. I receive a build of 4322, with a master and minor version of one, and 2032 for the revision number on my Windows XP machine. You can refer to the details of the Version object for more information.
Working with carriage returns
Displaying output often requires carriage returns to properly format the output. You may be unaware of the carriage return or new line character for the system, so using the Environment class helps ensure the proper value is utilized. The next example takes advantage of this property.
using System;
namespace BuilderExamples {
class Class1 {
static void Main(string[] args) {
String cr = Environment.NewLine;
String[] test = Environment.GetCommandLineArgs();
for (int x = 0; x < test.Length; x++) {
Console.Write(test[x] + cr);
} } } }
It is a simple example, but it does illustrate how you may utilize the platform's newline setting. While Windows machines are consistent, UNIX systems will be different, so converting to something like Mono will not be a problem. Also, it demonstrates accessing the command-line arguments with the first value in the string array being the program (assembly) name. The VB.NET equivalent follows:
Dim cr As String
Dim test() As String
Dim x As Integer
cr = Environment.NewLine
test = Environment.GetCommandLineArgs()
For x = 0 To (test.Length - 1)
Console.Write(test(x) + cr)
Next x
Inside a stacktrace
You are probably familiar with the contents of a stacktrace when an exception is encountered in an application. A stacktrace entry has the following format:
at FullClassName.MethodName(MethodParms) in FileName:line LineNumber
Here's a breakdown of what this line means:
The next C# line demonstrates a simple call to reveal a stacktrace when no error occurs:
Console.WriteLine(Environment.StackTrace.ToString());
With the following VB.NET equivalent:
Console.WriteLine(Environment.StackTrace.ToString())
This generates the following output on my test machine (for the VB.NET code):
at System.Environment.GetStackTrace(Exception
e)
at System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at BuilderStackTrace.Module1.Main() in
C:\BuilderStackTrace\Module1.vb:line 6
Know your environment
It is often important to know the details of the environment in which an application is running. This includes determining if the system is shutting down, environment variable values, and command-line arguments. The System.Environment class makes it easy to get these values.
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!
SponsoredWhite Papers, Webcasts, and Downloads
- Live Webcast: Simplified IT with Software-as-a-Service (SaaS) ZDNet
- Opening the Door to VoIP--and More Effective Phone Communications ShoreTel
- The Education Sector Rates Importance of IP Telephony Features, Management and Applications ShoreTel
- Live Webcast: Optimized Virtualization ZDNet
- Voice over IP and Productivity: How IP Telephony Enables Experts and Support Staff to Be Seemingly Everywhere at Once ShoreTel
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
