KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Code Snippet: How to get Windows Environment Variable Values
PRODUCT: 4D | VERSION: 12 | PLATFORM: Win
Published On: July 11, 2011

Sometimes it is useful for a developer to be aware of the Environment Variables that are set within the Windows Operating System that their application is running on. The following method is a general approach at returning this information:

If (Count parameters=1)
 C_TEXT($0;$1)
 C_TEXT($in;$out;$err;$findThis_t)
 $findThis_t:=$1+"="
 SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
 LAUNCH EXTERNAL PROCESS("cmd.exe /C set | findstr "+$findThis_t;$in;$out;$err)
 If (Position($findThis_t;$out)=0)
  ` variable not found
  $0:="**not found**"
 Else
  ` variable found
  $0:=Substring($out;Length($findThis_t)+1)
 End if
End if


When the method above is saved as UTIL_get_EnvironmentVar it can be called like this:

$localAppData:=UTIL_get_EnvironmentVar("LOCALAPPDATA")


In this situation, the return value may look like this:

C:\Users\username.domain\AppData\Local