An Interesting Aside Some Additional Members of the System Environment Class

The Environment type exposes a number of extremely helpful methods beyond GetCommandLineArgs(). Specifically, this class allows you to obtain a number of details regarding the operating system currently hosting your .NET application using various shared members. To illustrate the usefulness of System.Environment, update your Main() method with the following logic:

Shared Function Main(ByVal args As String()) As Integer ' OS running this app?

Console.WriteLine("Current OS: {0}", Environment.OSVersion) ' List the drives on this machine.

Dim drives As String() = Environment.GetLogicalDrives()

Dim d As String

For Each d In drives

Console.WriteLine("You have a drive named {0}.", d) Next

' Which version of the .NET platform is running this app?

Console.WriteLine("Executing version of .NET: {o}", _

Environment.Version) Return 0 End Function

Figure 3-5 shows a possible test run.

I*"** Fun with MainO ***** Arg: /argl Arg: -goamode lArg: -anotherArg fcurrent OS: Microsoft Windows NT 6.0.6000.0

You have a drive named C:\.

You have a drive named D:\.

You have a drive named E:\.

You have a drive named F:\.

Executing version of .NET: 2.0.50727.1433

Figure 3-5. Displaying system environment variables

The Environment type defines members other than those seen in the previous example. Table 3-1 documents some additional properties of interest; however, be sure to check out the .NET Framework 3.5 SDK documentation for full details.

Table 3-1. Select Properties of System.Environment


Meaning in Life







Gets the full path to the current application

Gets the name of the current machine

Gets the newline symbol for the current environment

Returns the number of processors on the current machine

Returns the full path to the system directory

Returns the name of the user who started this application

Source Code The FunWithMain project is located under the Chapter 3 subdirectory.

Was this article helpful?

0 0

Post a comment