该模块提供用户环境目录能力,用于获取用户的下载目录、桌面目录、文档目录的沙箱路径。上述三个方法分别适用于获取不同类型用户目录的场景,开发者可根据需要选择对应的目录类型。
本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
getUserDownloadDir(): string
获取当前用户的下载目录的沙箱路径。
需要权限:
系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain
设备行为差异:
返回值:
| 类型 | 说明 |
|---|---|
| string | 返回当前用户的下载目录的沙箱路径。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. 适用版本:11 |
| 801 | Capability not supported. |
| 13900042 | Unknown error. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
function getUserDownloadDirExample() {
try {
let path = Environment.getUserDownloadDir();
console.info(`Succeeded in getUserDownloadDir, path is ${path}`);
} catch (err) {
console.error(`Failed to getUserDownloadDir. Code: ${err.code}, message: ${err.message}`);
}
} getUserDesktopDir(): string
获取当前用户的桌面目录的沙箱路径。
需要权限:
系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain
设备行为差异:
返回值:
| 类型 | 说明 |
|---|---|
| string | 返回当前用户的桌面目录的沙箱路径。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. 适用版本:11 |
| 801 | Capability not supported. |
| 13900042 | Unknown error. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
function getUserDesktopDirExample() {
try {
let path = Environment.getUserDesktopDir();
console.info(`Succeeded in getUserDesktopDir, path is ${path}`);
} catch (err) {
console.error(`Failed to getUserDesktopDir. Code: ${err.code}, message: ${err.message}`);
}
} getUserDocumentDir(): string
获取当前用户的文档目录的沙箱路径。
需要权限:
系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain
设备行为差异:
返回值:
| 类型 | 说明 |
|---|---|
| string | 返回当前用户的文档目录的沙箱路径。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. 适用版本:11 |
| 801 | Capability not supported. |
| 13900042 | Unknown error. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
function getUserDocumentDirExample() {
try {
let path = Environment.getUserDocumentDir();
console.info(`Succeeded in getUserDocumentDir, path is ${path}`);
} catch (err) {
console.error(`Failed to getUserDocumentDir. Code: ${err.code}, message: ${err.message}`);
}
}