Built-In Classes: Environment
The .NET Framework provides a static class named Environment. This class quickly and easily gives some information about the application that is currently…
Introduction#
The Environment class is a static utility provided by the .NET Framework that offers a convenient way to retrieve details about the currently executing application.
Getting the Machine Name#
It used to be difficult to know the name of the computer on which your application is running. To let you easily get this information, the Environment class provides a property named MachineName.
Getting the Version of an Operating System#
To assist you with knowing the version of the operating system on which the application is running, the Environment class provides the OSVersion property. Here is an example of accessing it:
using System;
public class Exercise
{
static void Main()
{
Console.WriteLine("Command Line: {0}", Environment.OSVersion);
Environment.Exit(0);
}
}This would produce:
Command Line: Microsoft Windows NT 6.1.7600.0
Press any key to continue . . .This corresponds to Microsoft Windows 7.
Getting a User Name#
You can access the UserName property of the Environment class whenever you need to identify the individual currently logged into and using the system.
Getting the System Directory#
The Microsoft Windows operation system is usually installed on the C: drive with most of its drivers in a folder named System32 that is located either under Windows or WinNT. This full path of the driver's main folder is referred to as the system directory. To assist you with finding out the path to the system directory, the strong>Environment class is equipped with a property named SystemDirectory. Here is an example of accessing it:
using System;
public class Exercise
{
static void Main()
{
Console.WriteLine("Command Line: {0}", Environment.SystemDirectory);
Environment.Exit(0);
}
}This would produce:
Command Line: C:\Windows\system32
PPress any key to continue . . .This was run on Microsoft Windows 7 with default installation.
Creating a New Line#
You can use the \n escape sequence to send the caret to the next line. To support this operation, the Environment class is equipped with a property named NewLine. This performs the same operation as its escape sequence counterpart.
Getting the Directory of a Project#
In previous versions of Microsoft Visual Studio, one of the most annoying things (whether for a book writer or a learner) was that you always had to save a project, even if you only needed to test something. This is not the case in Microsoft Visual Studio 2010. Actually, a project must still have a location. This time, Microsoft Visual Studio uses a temporary folder to hold the project. If you don't save it and close the project, that temporary directory would be deleted. Whether you save the project or not, the Environment class allows you to know where the current project is located. To assist you with this, it is equipped with a property named CurrentDirectory. Here is an example of accessing it:
using System;
public class Exercise
{
static int Main()
{
Console.Write("Current Directory: ");
Console.WriteLine(Environment.CurrentDirectory);
return 0;
}
}The Exit Code of a Project#
The Main() function, which serves as the application's entry and exit point, can be declared as void if it does not need to return a specific exit code.
public static void Exit(int exitCode);The default value to pass to this method is 0. Here is an example of calling it:
using System;
public class Exercise
{
static void Main()
{
Environment.Exit(0);
}
}Home Copyright © 2010-2011 C# Key

