Environment Class in VB.NET

In this article I will explain you about the Environment Class in VB.NET.
  • 3373

he Environment class of the System namespace is handy for getting and setting various operating system-related information. You can use this class to retrieve information such as command-line arguments, exit codes, environment variable settings, contents of the call stack, time since last system boot in milliseconds (tick count), and version of the CLR. Members of the Environment class are described in Table 21.13.

Table 21.13: Environment Class Members

table21.13.gif

The System.Environment.SystemDirectory property returns a string containing the operating system's directory (e.g., c:\winnt\system32). You can use the Environment.UserInteractive property to get a Boolean value indicating whether the current process is running in user-interactive mode. (User-interactive mode means the user can send inputs to the process and see outputs with his or her eyes!) UserInteractive will be true if the current process is running in user-interactive mode.

To list the environment variables, open the command prompt console, type "SET," and press ENTER. You can obtain the values of any of these environment variables by using the Environment.GetEnvironmentVariable method. Simply pass it the name of the environment variable for which you want to obtain a value, and it will return the value as a string. For example, if you want the name of the user who is currently logged in, you can use the following:
          

    Environment.GetEnvironmentVariable("USERNAME")

You can use the Environment.NewLine property to get the newline string defined for this environment. (In our case it's a string containing \r\n.)

You can quit our application program anytime and return an Int32 exit code to the operating system, using the Environment.Exit(<exit_code>) method.

You can use the Environment.GetCommandLineArgs() method to return an array of strings where each element contains a command-line argument.

The example given below uses various Environment class methods, fields, and properties to illustrate the power of this class.

Example of Using the Environment Class

    Imports System.Diagnostics

    Namespace EnvironmentTestConsole
        ''' <summary>
        ''' Summary description for Class1.
        '''
</summary>
        Class Class1
            ''' <summary>
            ''' The main entry point for the application.
            '''
</summary>

            <STAThread()> _
            Public Shared Sub Main(ByVal args As String())
                ' reading the entire command line
                ' including the path to the application:
                Dim s As [String] = Environment.CommandLine
                Console.WriteLine(s)

                ' reading each argument individually:
                For Each s1 As [String] In Environment.GetCommandLineArgs()
                    Console.WriteLine(s1 & vbLf)
                Next

                ' reading a specific argument:
                If Environment.GetCommandLineArgs().Length > 0 Then
                    s = Environment.GetCommandLineArgs().GetValue(0).ToString()
                    Console.WriteLine(s & vbLf)
                End If

                ' manipulating the current working directory:
                Environment.CurrentDirectory = "C:\Temp"
                Console.WriteLine("Current directory is: " & Environment.CurrentDirectory)

                ' getting the computer and user names:
                Console.WriteLine("Machine= " & Environment.MachineName)
                Console.WriteLine("User= " & Environment.UserDomainName & "\" & Environment.UserName)
                Console.WriteLine("Is Interactive= " & Environment.UserInteractive.ToString())

                ' reading all environment variables
                For Each s2 As [String] In Environment.GetEnvironmentVariables().Keys
                    Console.WriteLine(s2 & "=" & Environment.GetEnvironmentVariable(s2).ToString())
                Next

                ' reading a specific variable
                s = Environment.GetEnvironmentVariable("PATH").ToString()

                ' translating or expanding strings containing
                ' variable references
                Console.WriteLine(Environment.ExpandEnvironmentVariables("User%userdomain%\%username% on %computername%\n"))

                ' identifying logical drive letters
                For Each s3 As [String] In Environment.GetLogicalDrives()
                    Console.WriteLine("Drive: " & s3 & vbLf)
                Next

                ' locating the system folder
                Console.WriteLine("System Dir= " & Environment.SystemDirectory & vbLf)
               
                ' locating all system and other special folders
\
                Dim sFolderName As [String] = ""
                Dim sFolderPath As [String] = ""

                For Each eFolderID As Environment.SpecialFolder In [Enum].GetValues(GetType(System.Environment.SpecialFolder))
                    sFolderName =
[Enum].GetName(GetType(System.Environment.SpecialFolder), eFolderID)
                    sFolderPath =
Environment.GetFolderPath(eFolderID)
                    Console.WriteLine(sFolderName & "=" & sFolderPath & vbLf)
                Next

                ' locating a specific special folder
                sFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

                ' identifying OS parameters:
                s = Environment.NewLine

                ' New Line sequence for the
                ' current OS
                Select Case Environment.OSVersion.Platform
                    Case PlatformID.Win32NT
                        Console.WriteLine("Running under Windows NT or Windows 2000" & vbLf)
                        Exit Select
                    Case
PlatformID.Win32S
                        Console.WriteLine("Running under Win32s" & vbLf)
                        Exit Select
                    Case
PlatformID.Win32Windows
                        Console.WriteLine("Running under win9x" & vbLf)
                        Exit Select
                End
Select

                Console.WriteLine("OS Version= " & Environment.OSVersion.Version.ToString() & vbLf)
                Console.WriteLine("Stack size= " & Environment.StackTrace & vbLf)
                Console.WriteLine("Tick Count= " & Environment.TickCount.ToString() & vbLf)
                Debug.WriteLine("CLR Version= " & Environment.Version.ToString() & vbLf)
                Debug.WriteLine("WorkingSet size= " & Environment.WorkingSet.ToString() & vbLf)

                ' setting an exit code for the current process
                Environment.ExitCode = 19
                Console.WriteLine("Exit code=" & Environment.ExitCode.ToString() & vbLf)
                Console.WriteLine(vbLf & "Hit any key to continue" & vbLf)
                Console.ReadLine()

                'Setting an exit code and terminating immediately:
                Environment.[Exit](1919)

                ' the exit codes are ignored in debugging and are only valid
                ' for releases.
                Environment.GetEnvironmentVariable("USERNAME")
            End Sub
        End
Class
    End
Namespace

Output

ec1.gif

Conclusion

Hope this article would have helped you in understanding the Environment Class in VB.NET

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.