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

  • 2025-06-12
  • 浏览 (4)

@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 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { Environment } from '@kit.CoreFileKit';

Environment.getUserDownloadDir

getUserDownloadDir(): string

Obtains the sandbox path of the pre-authorized Download directory. Currently, only 2-in-1 devices are supported.

System capability: SystemCapability.FileManagement.File.Environment.FolderObtain

Return value

Type Description
string Sandbox path of the Download directory obtained.

Error codes

For details about the error codes, see File Management Error Codes.

ID Error Message
801 Capability not supported.
13900042 Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
function getUserDownloadDirExample() {
  try {
    let path = Environment.getUserDownloadDir();
    console.log(`success to getUserDownloadDir: ${JSON.stringify(path)}`);
  } catch (error) {
    console.error(`failed to getUserDownloadDir, Error code: ${error.code}, message: ${error.message}`);
  }
}

Environment.getUserDesktopDir

getUserDesktopDir(): string

Obtains the sandbox path of the pre-authorized Desktop directory. Currently, only 2-in-1 devices are supported.

System capability: SystemCapability.FileManagement.File.Environment.FolderObtain

Return value

Type Description
string Sandbox path of the Desktop directory obtained.

Error codes

For details about the error codes, see File Management Error Codes.

ID Error Message
801 Capability not supported.
13900042 Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
function getUserDesktopDirExample() {
  try {
    let path = Environment.getUserDesktopDir();
    console.log(`success to getUserDesktopDir: ${JSON.stringify(path)}`);
  } catch (error) {
    console.error(`failed to getUserDesktopDir, Error code: ${error.code}, message: ${error.message}`);
  }
}

Environment.getUserDocumentDir

getUserDocumentDir(): string

Obtains the sandbox path of the pre-authorized Document directory. Currently, only 2-in-1 devices are supported.

System capability: SystemCapability.FileManagement.File.Environment.FolderObtain

Return value

Type Description
string Sandbox path of the Documents directory obtained.

Error codes

For details about the error codes, see File Management Error Codes.

ID Error Message
801 Capability not supported.
13900042 Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
function getUserDocumentDirExample() {
  try {
    let path = Environment.getUserDocumentDir();
    console.log(`success to getUserDocumentDir: ${JSON.stringify(path)}`);
  } catch (error) {
    console.error(`failed to getUserDocumentDir, Error code: ${error.code}, message: ${error.message}`);
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

0  赞