harmony 鸿蒙@ohos.file.environment (Directory Environment Capability)

  • 2023-02-17
  • 浏览 (551)

@ohos.file.environment (Directory Environment Capability)

The Environment module provides APIs for obtaining the root directories of the storage and user 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.

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.

Error codes

For details about the error codes, see File Management Error Codes. |ID |Error Message | |—————————-|———-| |202|The caller is not a system application| |13900020|Invalid argument| |13900042|Unknown error|

Example

  import { BusinessError } from '@ohos.base';
  environment.getStorageDataDir().then((path: string) => {
      console.info("getStorageDataDir successfully, Path: " + path);
  }).catch((err: BusinessError) => {
      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.

Error codes

For details about the error codes, see File Management Error Codes. |ID |Error Message | |—————————-|———-| |202|The caller is not a system application| |13900020|Invalid argument| |13900042|Unknown error|

Example

  import { BusinessError } from '@ohos.base';
  environment.getStorageDataDir((err: BusinessError, path: string) => {
    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 user files. 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 user files.

Error codes

For details about the error codes, see File Management Error Codes. |ID |Error Message | |—————————-|———-| |202|The caller is not a system application| |13900020|Invalid argument| |13900042|Unknown error|

Example

  import { BusinessError } from '@ohos.base';
  environment.getUserDataDir().then((path: string) => {
    console.info("getUserDataDir successfully, Path: " + path);
  }).catch((err: BusinessError) => {
    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 user 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 user files.

Error codes

For details about the error codes, see File Management Error Codes. |ID |Error Message | |—————————-|———-| |202|The caller is not a system application| |13900020|Invalid argument| |13900042|Unknown error|

Example

  import { BusinessError } from '@ohos.base';
  environment.getUserDataDir((err: BusinessError, path: string) => {
    if (err) {
      console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("getUserDataDir successfully, Path: " + path);
    }
  });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞