该模块提供环境目录能力:获取内存存储根目录、公共文件根目录的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主页,持续获取:
在这里插入图片描述

最新鸿蒙技术文章
独家项目实战教程
免费直播/训练营信息
👉 下方资源区已为你备好!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐