nsIEnvironment

Scriptable access to the current process environment.
Inherits from: nsISupports Last changed in Gecko 1.7

Implemented by: @mozilla.org/process/environment;1 as a service:

var env = Components.classes["@mozilla.org/process/environment;1"].
          getService(Components.interfaces.nsIEnvironment);

Method overview

void set(in AString aName, in AString aValue);
AString get(in AString aName);
boolean exists(in AString aName);

Methods

set()

Set the value of an environment variable.

 void set(
   in AString aName,
   in AString aValue
 );
Parameters
aName
The variable name to set.
aValue
The value to set.

get()

Get the value of an environment variable.

 AString get(
   in AString aName
 );
Parameters
aName

the variable name to retrieve.

Return value

Returns the value of the env variable. An empty string will be returned when the env variable does not exist or when the value itself is an empty string - please use exists() to probe whether the env variable exists or not.

exists()

Check the existence of an environment variable. This method checks whether an environment variable is present in the environment or not.

Note: * For Unix/Linux platforms we follow the Unix definition: An environment variable exists when getenv() returns a non-NULL value. An environment variable does not exist when getenv() returns NULL.

  • For non-Unix/Linux platforms we have to fall back to a "portable" definition (which is incorrect for Unix/Linux!!!!) which simply checks whether the string returned by Get() is empty or not.
 boolean exists(
   in AString aName
 );
Parameters
aName
The variable name to probe.
Return value

If the variable has been set, the value returned is PR_TRUE. If the variable was not defined in the environment PR_FALSE will be returned.

Examples

Windows

This example gets the path to the Porgram Files directory on Windows. Credit to morat from mozillazine.

var nsIEnvironment = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment);
var pathToProgramFiles = nsIEnvironment.get("ProgramFiles"); //for me this returns "C:\Program Files"