Home

Built-In Classes: Environment

     

Introduction

The .NET Framework provides a static class named Environment. This class quickly and easily gives some information about the application that is currently running and some objects you can use in your application. The Environment class is defined in the System namespace. This means that you don't have to do anything to access it, since it is automatically available whenever you create a C# 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

At any time, if you want to know the user name of the person who is currently using your application, you can get the value of the UserName property of the Environment class.

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

You can mark the Main() function as void, which indicates that it doesn't return any value. Also, the Main() function represents the entry and the exit point of an application. As a matter of fact, when Main() ends, you can specify a certain number by which it exits. This information can be used by other applications, however they want to use it. To assist you with this, the Environment class provides a property named ExitCode.

To let you specify the exit number of your application, the Environment class is equipped with a method named Exit. Its syntax is:

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-2016, FunctionX