ARTICLE

Retrieving Environment variables in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 October 15, 2003
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.
 
Reader Level:

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();
}
}

share this article :
post comment
 

IF you want to display System Information in window Application in VB.Net then follow the following Steps:

Step  1: Add a ListBox

Step  2: Declare the following at the top of the form

Imports System.Environment

Step 3: Copy the following code and paste it

    Private Sub GetEnvironmentVariables()

        Dim dictEntry As System.Collections.DictionaryEntry

        For Each dictEntry In Environment.GetEnvironmentVariables()

            Me.ListBox1.Items.Add("Key : " & (dictEntry.Key.ToString()) & "Value: " & (dictEntry.Value.ToString()))

        Next

    End Sub

Step 4: Now call this methos on either form load or on button click:

I have called this method on form load

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GetEnvironmentVariables()

    End Sub

Now run the Project

And if you only wanted to display a certain Value like ComputerName or UserName then follow the following Steps:

Step  1: Add a textbox

Step  2: Declare the following at the top of the form

Imports System.Environment

Step 3: Copy the following code and paste it

    Private Sub GetEnvironmentVariables()

        Dim StrUserName As String

        StrUserName = Environment.GetEnvironmentVariable("USERNAME")

        Me.TextBox1.Text = StrUserName

    End Sub

Step 4: Now call this methos on either form load or on button click:

I have called this method on form load

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GetEnvironmentVariables()

    End Sub

Now run the Project

Posted by Amber Naz May 01, 2010

Hi, I need the same code but for .net framework windows CE

Posted by Ricardo Martinez Feb 15, 2007
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor