SetEnvironmentVariable Function

Declare Function SetEnvironmentVariable Lib "kernel32.dll" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long

Platforms

Description & Usage

SetEnvironmentVariable sets the value of one of the computer's environment variables.

Return Value

If successful, the function returns a nonzero value. If an error occured, the function returns zero (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

lpName
The name of the environment variable to set.
lpValue
The value to give to the environment variable.

Example

Change the value of the TEMP and TMP environment variables, which refer to the directory used to store temporary files, to "D:\Temp". This probably isn't the best way to try to change the temporary file directory, but remember, this is just an example. The example runs when the user clicks button Command1. Naturally, to use this example, you must place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SetEnvironmentVariable Lib "kernel32.dll" Alias "SetEnvironmentVariableA" (ByVal _
	lpName As String, ByVal lpValue As String) As Long

' *** Place the following code inside the form window. ***

Private Sub Command1_Click()
	Dim retval As Long  ' return value
	
	' Change the value of the TEMP and TMP environment variables.
	retval = SetEnvironmentVariable("TEMP", "D:\Temp")
	retval = SetEnvironmentVariable("TMP", "D:\Temp")
	' That's all there is to it!
End Sub

See Also

GetEnvironmentVariable

Category

Processes & Threads

Back to the Function list.
Back to the Reference section.


Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/setenvironmentvariable.html