System.EnvironmentVariable

From Xojo Documentation

Revision as of 21:06, 9 December 2020 by PLefebvre (talk | contribs)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

System.EnvironmentVariable(Name as String) As String

Supported on Desktop, Web, Console.

Used to get and set environment variables. Name is case-sensitive.

Notes

For web applications, you can use this method to get the "ROOT" or "DOCUMENT_ROOT" environment variables used by the web server. The "env" example below can be used to list all the available environment variables on your web server.

Sample Code

Get the value of the HOME environment variable:

Var s As String
s = System.EnvironmentVariable("HOME")

MessageBox(s)

Set the value of the HOME environment variable:

System.EnvironmentVariable("HOME") = "/Users/username"

To get the list of environment variables and their values (macOS and Linux), execute the "env" command from a terminal window or via the Shell class. On Windows, use the "set" command instead of "env".

Var sh As New Shell
Var cmd As String
#If TargetWindows Then
cmd = "set"
#Else
cmd = "env"
#EndIf

sh.Execute(cmd)
TextArea1.Value = sh.Result

This example gets the value of the COMPUTERNAME environment variable on Windows.

#If TargetWindows Then
TextField1.Value = System.EnvironmentVariable("COMPUTERNAME")
#EndIf