鸿蒙API14开发【@ohos.file.environment (目录环境能力)(系统接口)】文件基础服务
本文介绍了Environment模块提供的环境目录获取能力,包含获取内存存储根目录和公共文件根目录的JS接口。该模块支持通过Promise和Callback两种异步方式调用,适用于系统应用开发。主要接口包括:getStorageDataDir()获取内存存储根目录、getUserDataDir()获取公共文件根目录,以及11+版本新增的getExternalStorageDir()获取外卡根目录沙
该模块提供环境目录能力:获取内存存储根目录、公共文件根目录的JS接口。
导入模块
import { Environment } from '@kit.CoreFileKit';
ts
environment.getStorageDataDir
getStorageDataDir():Promise
异步方法获取内存存储根目录,以promise形式返回结果。
系统能力:SystemCapability.FileManagement.File.Environment
系统接口:此接口为系统接口。
返回值:
| 类型 | 说明 |
|---|---|
| Promise | 返回存储根目录。 |
| 错误码ID | 错误信息 |
|---|---|
| 202 | The caller is not a system application |
| 13900020 | Invalid argument |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
Environment.getStorageDataDir().then((path: string) => {
console.info("getStorageDataDir successfully, Path: " + path);
}).catch((err: BusinessError) => {
console.error("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
});
ts
environment.getStorageDataDir
getStorageDataDir(callback:AsyncCallback):void
异步方法获取内存存储根目录,以callback形式返回结果。
系统能力:SystemCapability.FileManagement.File.Environment
系统接口:此接口为系统接口。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback | 是 | 异步获取内存存储根目录之后的回调。 |
| 错误码ID | 错误信息 |
|---|---|
| 202 | The caller is not a system application |
| 13900020 | Invalid argument |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
Environment.getStorageDataDir((err: BusinessError, path: string) => {
if (err) {
console.error("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("getStorageDataDir successfully, Path: " + path);
}
});
ts
environment.getUserDataDir
getUserDataDir():Promise
异步方法获取公共文件根目录,以promise形式返回结果。
系统能力:SystemCapability.FileManagement.File.Environment
系统接口:此接口为系统接口。
返回值:
| 类型 | 说明 |
|---|---|
| Promise | 返回公共文件根目录。 |
| 错误码ID | 错误信息 |
|---|---|
| 202 | The caller is not a system application |
| 13900020 | Invalid argument |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
Environment.getUserDataDir().then((path: string) => {
console.info("getUserDataDir successfully, Path: " + path);
}).catch((err: BusinessError) => {
console.error("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
});
ts
environment.getUserDataDir
getUserDataDir(callback:AsyncCallback): void
异步方法获取公共文件根目录,以callback形式返回结果。
系统能力:SystemCapability.FileManagement.File.Environment
系统接口:此接口为系统接口。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback | 是 | 异步获取公共文件根目录之后的回调。 |
| 错误码ID | 错误信息 |
|---|---|
| 202 | The caller is not a system application |
| 13900020 | Invalid argument |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
Environment.getUserDataDir((err: BusinessError, path: string) => {
if (err) {
console.error("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("getUserDataDir successfully, Path: " + path);
}
});
ts
environment.getExternalStorageDir11+
getExternalStorageDir(): string
获取外卡根目录的沙箱路径,该接口仅对具有该系统能力的设备开放。
需要权限:ohos.permission.FILE_ACCESS_MANAGER
系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain
系统接口:此接口为系统接口。
返回值:
| 类型 | 说明 |
|---|---|
| string | 返回外卡根目录的沙箱路径。 |
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 801 | Capability not supported. |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
function getExternalStorageDirExample() {
try {
let path = Environment.getExternalStorageDir();
console.log(`success to getExternalStorageDir: ${JSON.stringify(path)}`);
} catch (error) {
console.error(`failed to getExternalStorageDir because: ${JSON.stringify(error)}`);
}
}
ts
environment.getUserHomeDir11+
getUserHomeDir(): string
获取当前用户下应用沙箱路径的内卡目录,该接口仅对具有该系统能力的设备开放。
需要权限:ohos.permission.FILE_ACCESS_MANAGER
系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain
系统接口:此接口为系统接口。
返回值:
| 类型 | 说明 |
|---|---|
| string | 返回当前用户下应用沙箱路径的内卡目录。 |
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 801 | Capability not supported. |
| 13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
function getUserHomeDirExample() {
try {
let path = Environment.getUserHomeDir();
console.log(`success to getUserHomeDir: ${JSON.stringify(path)}`);
} catch (error) {
console.error(`failed to getUserHomeDir because: ${JSON.stringify(error)}`);
}
}
加入鸿蒙学习阵营!
除了本文,面费领《HarmonyOS应用开发》+《鸿蒙openharmony系统开发大全》 等入门进阶资料!关注我的CSDN主页,持续获取:
最新鸿蒙技术文章
独家项目实战教程
免费直播/训练营信息
👉 下方资源区已为你备好!
更多推荐


所有评论(0)