@ohos.file.environment (Directory Environment Capability)

The Environment module provides APIs for obtaining the root directories of the storage and public files.

NOTE

  • The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs of this module are system APIs and cannot be called by third-party applications.
  • The APIs of this module support processing of error codes. For details, see File Management Error Codes.

Modules to Import

import environment from '@ohos.file.environment';

environment.getStorageDataDir

getStorageDataDir():Promise<string>

Obtains the root directory of the storage. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.Environment

Return value

Type Description
Promise<string> Promise used to return the root directory of the storage.

Example

environment.getStorageDataDir().then((path) => {
    console.info("getStorageDataDir successfully, Path: " + path);
}).catch((err) => {
    console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
});

environment.getStorageDataDir

getStorageDataDir(callback:AsyncCallback<string>):void

Obtains the root directory of the storage. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.File.Environment

Parameters

Name Type Mandatory Description
callback AsyncCallback<string> Yes Asynchronous callback invoked to return the root directory of the storage.

Example

environment.getStorageDataDir((err, path) => {
  if (err) {
    console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("getStorageDataDir successfully, Path: " + path);
  }
});

environment.getUserDataDir

getUserDataDir():Promise<string>

Obtains the root directory of public files. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.Environment

Return value

Type Description
Promise<string> Promise returned with the root directory of public files.

Example

environment.getUserDataDir().then((path) => {
  console.info("getUserDataDir successfully, Path: " + path);
}).catch((err) => {
  console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
});

environment.getUserDataDir

getUserDataDir(callback:AsyncCallback<string>): void

Obtains the root directory of public files. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.File.Environment

Parameters

Name Type Mandatory Description
callback AsyncCallback<string> Yes Asynchronous callback used to return the root directory of public files.

Example

environment.getUserDataDir((err, path) => {
  if (err) {
    console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("getUserDataDir successfully, Path: " + path);
  }
});