Environment


Visual Basic provides a couple of tools for working with the application’s environment. The following sections describe two: the Environ function and the System.Environment object.

Environ

Environment variables are normally set on a system-wide basis before the program begins. In older operating systems, batch files such as autoexec.bat set these values. More recent systems provide Control Panel tools to set environment variables.

Windows XP

In Windows XP, open the Control Panel, run the System applet, select the Advanced tab to see the page shown in Figure 27-1. Alternatively, you can right-click My Computer and select Properties from the context menu.

image from book
Figure 27-1: In Windows XP, click the Advanced tab to see the Environment Variables button.

Click the Environment Variables button to display the dialog box shown in Figure 27-2. Use the dialog box to add, modify, or remove environment variables.

image from book
Figure 27-2: Use this Windows XP dialog box to add, modify, and delete environment variables.

Tip 

Environment variables are loaded when a process starts, and they are inherited by any process launched by the initial process. For Visual Basic development, that means the variables are loaded when you start Visual Studio and they are inherited by the program you are working on when you start it. That means if you make changes to the system’s environment variables, you need to close and reopen Visual Studio before your program will see the changes.

Windows Vista

In Windows Vista, open the Control Panel and navigate to System and Maintenance image from book System and click the Advanced system settings link in tasks list on the left to display the dialog shown in Figure 27-3.

image from book
Figure 27-3: In Windows Vista, use the Advanced tab to see the Environment Variables button.

Click the Environment Variables button to display the dialog box shown in Figure 27-4. Use the dialog box to add, modify, or remove environment variables.

image from book
Figure 27-4: Use this Windows Vista dialog box to add, modify, and delete environment variables in Windows Vista.

At runtime, a Visual Basic application can use the Environ function to retrieve environment variable values. If you pass this function a number, it returns a string giving the statement that assigns the corresponding environment variable. For example, Environ(1) might return the following string:

  ALLUSERSPROFILE=C:\Documents and Settings\All Users 

You should pass the function a number between 1 and 255. Environ returns a zero-length string if the number does not correspond to an environment variable. The following code uses this fact to list all the application’s environment variables. When it finds a variable that has zero length, it knows it has read all of the variables with values.

  For i As Integer = 1 To 255     If Environ(i).Length = 0 Then Exit For     Debug.WriteLine(Environ(i)) Next i 

If you pass the Environ function the name of an environment variable, the function returns the variable’s value or Nothing if the variable does not exist. The following code displays the value assigned to the USERNAME variable:

  MessageBox.Show(Environ("USERNAME"))  

System.Environment

The Environ function is easy to use, but it’s not very flexible; it cannot create or modify variable values. Setting and modifying environment variables isn’t as useful as it might seem, however, because each process has its own environment variables. If an application creates a new variable, other processes will not see it anyway. The only way another process will see the variable is if an application sets the variable’s value and then starts the new process (for example, by using the Shell statement).

The System.Environment object provides methods for getting and setting environment variables. It also provides properties and methods for working with many other items in the application’s environment. The following table describes the Environment object’s most useful properties.

Open table as spreadsheet

Property

Purpose

CommandLine

Returns the process’s command line.

CurrentDirectory

Gets or sets the fully qualified path to the current directory.

ExitCode

Gets or sets the process’s exit code. If the program starts from a Main function, that function’s return value also sets the exit code.

HasShutdownStarted

Returns True if the common language runtime is shutting down.

MachineName

Returns the computer’s NetBIOS name.

NewLine

Returns the environment’s defined new line string. For example, this might be a carriage return followed by a line feed.

OSVersion

Returns an OperatingSystem object containing information about the operating system. This object provides the properties ServicePack (name of the most recent service pack installed), Version (includes Major, Minor, Build, and Revision; ToString combines them all), VersionString (combines the operating system name, version, and most recent service pack), and Platform, which can be Unix, Win32NT (Windows NT or later), Win32S (runs on 16-bit Windows to provide access to 32-bit applications), Win32Windows (Windows 95 or later), or WinCE.

ProcessorCount

Returns the number of processors on the computer.

StackTrace

Returns a string describing the current stack trace.

SystemDirectory

Returns the system directory’s fully qualified path.

TickCount

Returns the number of milliseconds that have elapsed since the system started.

UserDomainName

Returns the current user’s network domain name.

UserInteractive

Returns True if the process is interactive. This only returns False if the application is a service process or web service.

UserName

Returns the name of the user who started the process.

Version

Returns a Version object describing the Common Language Runtime. This object provides the properties Major, Minor, Build, and Revision. Its ToString method combines them all.

WorkingSet

Returns the amount of physical memory mapped to this process in bytes.

The following table describes the Environment object’s most useful methods.

Open table as spreadsheet

Method

Purpose

Exit

Ends the process immediately. Form Closing and Closed event handlers do not execute.

ExpandEnvironmentVariables

Replaces environment variable names in a string with their values. For example, the following code displays the current user’s name. MessageBox.Show(“I am %username%.”)

GetCommandLineArgs

Returns an array of strings containing the application’s command-line arguments. The first entry (with index 0) is the name of the program’s executable file.

GetEnvironmentVariable

Returns an environment variable’s value.

GetEnvironmentVariables

Returns an IDictionary object containing the names and values of all environment variables.

GetFolderPath

Returns the path to a system folder. This method’s parameter is a SpecialFolder enumeration value such as Cookies, Desktop, SendTo, or Recent. See the online help for a complete list of available folders.

GetLogicalDrives

Returns an array of strings containing the names of the logical drives on the current computer.

SetEnvironmentVariable

Creates, modifies, or deletes an environment variable.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net