Retrieving Environment Variables in VB.NET
In one of my recent applications, I needed machine name, program files folder, and other system related variables. The System.Environment class provides handy members to do so. In this article, I will talk about the Environment class and its members and how to use them in your applications.
In one of my recent applications, I needed machine name, program files folder, and other system related variables. The System.Environment class provides handy members to do so. In this article, I will talk about the Environment class and its members and how to use them in your applications.
The Environment class defined in the System namespace allows developers to get the information about the current environment and platform. Listing 1 describes some of the Environment class properties.
CurrentDirectory |
Returns and sets the fully qualified path of the current directory; that is, the directory from which this process starts. |
MachineName |
Returns the NetBIOS name of this local computer. |
NewLine |
Returns the newline string defined for this environment. |
OSVersion |
Returns an OperatingSystem object that contains the current platform identifier and version number. |
SystemDirectory |
Returns the fully qualified path of the system directory. |
UserDomainName |
Returns the network domain name associated with the current user. |
UserName |
Returns the user name of the person who started the current thread. |
Version |
Returns a Version object that describes the major, minor, build, and revision numbers of the common language runtime. |
WorkingSet |
Returns the amount of physical memory mapped to the process context. |
The Environment class also provides some useful methods. Listing 2 shows some of these methods.
ExpandEnvironmentVariables |
Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string. |
GetCommandLineArgs |
Returns a string array containing the command line arguments for the current process. |
GetEnvironmentVariable |
Returns the value of the specified environment variable. |
GetEnvironmentVariables |
Returns all environment variables and their values. |
GetFolderPath |
Gets the path to the system special folder identified by the specified enumeration. |
GetLogicalDrives |
Returns an array of string containing the names of the logical drives on the current computer. |
Now let's write a simple program that will use some of the Environment class properties and methods. The following code displays the machine information and logical drives of a machine.
VB.NET Code:
Imports System
Module Module1
Sub Main()
' Getting machine and user information
Console.WriteLine("Machine Information")
Console.WriteLine("======================")
Console.WriteLine("Machine Name: " + Environment.MachineName)
Console.WriteLine("OS Version: " & Environment.OSVersion.ToString())Console.WriteLine("System Directory: " + Environment.SystemDirectory)Console.WriteLine("User Name: " + Environment.UserName)
Console.WriteLine("Version: " + Environment.Version.ToString())
Console.WriteLine("Current Directory: " + Environment.CurrentDirectory)
Console.WriteLine()
' Get all logical hard drives
Dim drives As String()
Dim counter As Int32
drives = Environment.GetLogicalDrives()
Console.WriteLine("======================")
Console.WriteLine("Available drives:")
For counter = 0 To drives.Length - 1
Console.WriteLine(drives(counter).ToString())
Next
Console.ReadLine()
End Sub
End Module
C# Code:
using System;
class EnvironmentClass
{
static void Main(string[] args)
{
//Getting machine and user information
Console.WriteLine("Machine Information");
Console.WriteLine("======================");
Console.WriteLine("Machine Name: "+ Environment.MachineName);
Console.WriteLine("OS Version: "+ Environment.OSVersion);
Console.WriteLine("System Directory: "+ Environment.SystemDirectory);
Console.WriteLine("User Name: "+ Environment.UserName);
Console.WriteLine("Version: "+ Environment.Version);
Console.WriteLine("Current Directory: "+ Environment.CurrentDirectory);
Console.WriteLine();
// Get all logical hard drives
string[] drives = Environment.GetLogicalDrives();
Console.WriteLine("======================");
Console.WriteLine("Available drives:");
foreach(string drive in drives)
Console.WriteLine(drive);
Console.ReadLine();
}
}