nsIEnvironment 编辑

xpcom/threads/nsIEnvironment.idlScriptable 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"

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:49 次

字数:4440

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文