1# @ohos.file.environment (目录环境能力)(系统接口) 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @wangke25; @gsl_1234; @wuchengjun5--> 5<!--Designer: @gsl_1234; @wangke25--> 6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang--> 7<!--Adviser: @foryourself--> 8 9该模块提供环境目录能力:获取内存存储根目录、公共文件根目录的JS接口。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.file.environment (目录环境能力)](js-apis-file-environment-sys.md)。 15 16## 导入模块 17 18```ts 19import { Environment } from '@kit.CoreFileKit'; 20``` 21 22## environment.getStorageDataDir 23 24getStorageDataDir():Promise<string> 25 26异步方法获取内存存储根目录,以promise形式返回结果。 27 28**系统能力**:SystemCapability.FileManagement.File.Environment 29 30**系统接口**:此接口为系统接口。 31 32**返回值:** 33 34| 类型 | 说明 | 35| --------------------- | ---------------- | 36| Promise<string> | 返回存储根目录。 | 37 38**错误码:** 39 40以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 41| 错误码ID | 错误信息 | 42| ---------------------------- | ---------- | 43| 202 | The caller is not a system application | 44| 13900020 | Invalid argument | 45| 13900042 | Unknown error | 46 47**示例:** 48 49 ```ts 50 import { BusinessError } from '@kit.BasicServicesKit'; 51 Environment.getStorageDataDir().then((path: string) => { 52 console.info("getStorageDataDir successfully, Path: " + path); 53 }).catch((err: BusinessError) => { 54 console.error("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code); 55 }); 56 ``` 57 58## environment.getStorageDataDir 59 60getStorageDataDir(callback:AsyncCallback<string>):void 61 62异步方法获取内存存储根目录,以callback形式返回结果。 63 64**系统能力**:SystemCapability.FileManagement.File.Environment 65 66**系统接口**:此接口为系统接口。 67 68**参数:** 69 70| 参数名 | 类型 | 必填 | 说明 | 71| -------- | --------------------------- | ---- | -------------------------------- | 72| callback | AsyncCallback<string> | 是 | 异步获取内存存储根目录之后的回调。 | 73 74**错误码:** 75 76以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 77| 错误码ID | 错误信息 | 78| ---------------------------- | ---------- | 79| 202 | The caller is not a system application | 80| 13900020 | Invalid argument | 81| 13900042 | Unknown error | 82 83**示例:** 84 85 ```ts 86 import { BusinessError } from '@kit.BasicServicesKit'; 87 Environment.getStorageDataDir((err: BusinessError, path: string) => { 88 if (err) { 89 console.error("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code); 90 } else { 91 console.info("getStorageDataDir successfully, Path: " + path); 92 } 93 }); 94 ``` 95 96## environment.getUserDataDir 97 98getUserDataDir():Promise<string> 99 100异步方法获取公共文件根目录,以promise形式返回结果。 101 102**系统能力**:SystemCapability.FileManagement.File.Environment 103 104**系统接口**:此接口为系统接口。 105 106**返回值:** 107 108| 类型 | 说明 | 109| --------------------- | ------------------ | 110| Promise<string> | 返回公共文件根目录。 | 111 112**错误码:** 113 114以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 115| 错误码ID | 错误信息 | 116| ---------------------------- | ---------- | 117| 202 | The caller is not a system application | 118| 13900020 | Invalid argument | 119| 13900042 | Unknown error | 120 121**示例:** 122 123 ```ts 124 import { BusinessError } from '@kit.BasicServicesKit'; 125 Environment.getUserDataDir().then((path: string) => { 126 console.info("getUserDataDir successfully, Path: " + path); 127 }).catch((err: BusinessError) => { 128 console.error("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code); 129 }); 130 ``` 131 132## environment.getUserDataDir 133 134getUserDataDir(callback:AsyncCallback<string>): void 135 136异步方法获取公共文件根目录,以callback形式返回结果。 137 138**系统能力**:SystemCapability.FileManagement.File.Environment 139 140**系统接口**:此接口为系统接口。 141 142**参数:** 143 144| 参数名 | 类型 | 必填 | 说明 | 145| -------- | --------------------------- | ---- | -------------------------------- | 146| callback | AsyncCallback<string> | 是 | 异步获取公共文件根目录之后的回调。 | 147 148**错误码:** 149 150以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 151| 错误码ID | 错误信息 | 152| ---------------------------- | ---------- | 153| 202 | The caller is not a system application | 154| 13900020 | Invalid argument | 155| 13900042 | Unknown error | 156 157**示例:** 158 159 ```ts 160 import { BusinessError } from '@kit.BasicServicesKit'; 161 Environment.getUserDataDir((err: BusinessError, path: string) => { 162 if (err) { 163 console.error("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code); 164 } else { 165 console.info("getUserDataDir successfully, Path: " + path); 166 } 167 }); 168 ``` 169 170## environment.getExternalStorageDir<sup>11+</sup> 171 172getExternalStorageDir(): string 173 174获取外卡根目录的沙箱路径,该接口仅对具有该系统能力的设备开放。 175 176**需要权限**:ohos.permission.FILE_ACCESS_MANAGER 177 178**系统能力**:SystemCapability.FileManagement.File.Environment.FolderObtain 179 180**系统接口**:此接口为系统接口。 181 182**返回值:** 183 184| 类型 | 说明 | 185| --------------------- |---------------| 186| string | 返回外卡根目录的沙箱路径。 | 187 188**错误码:** 189 190以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 191 192| 错误码ID | 错误信息 | 193| ---------------------------- | --------- | 194| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 195| 202 | Permission verification failed, application which is not a system application uses system API. | 196| 801 | Capability not supported. | 197| 13900042 | Unknown error | 198 199**示例:** 200 201```ts 202import { BusinessError } from '@kit.BasicServicesKit'; 203function getExternalStorageDirExample() { 204 try { 205 let path = Environment.getExternalStorageDir(); 206 console.info(`Succeeded in getExternalStorageDir, path is ${path}`); 207 } catch (err) { 208 console.error(`Failed to getExternalStorageDir. Code: ${err.code}, message: ${err.message}`); 209 } 210} 211``` 212 213## environment.getUserHomeDir<sup>11+</sup> 214 215getUserHomeDir(): string 216 217获取当前用户下应用沙箱路径的内卡目录,该接口仅对具有该系统能力的设备开放。 218 219**需要权限**:ohos.permission.FILE_ACCESS_MANAGER 220 221**系统能力**:SystemCapability.FileManagement.File.Environment.FolderObtain 222 223**系统接口**:此接口为系统接口。 224 225**返回值:** 226 227| 类型 | 说明 | 228| --------------------- |-----------------| 229| string | 返回当前用户下应用沙箱路径的内卡目录。 | 230 231**错误码:** 232 233以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 234 235| 错误码ID | 错误信息 | 236| ---------------------------- | --------- | 237| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 238| 202 | Permission verification failed, application which is not a system application uses system API. | 239| 801 | Capability not supported. | 240| 13900042 | Unknown error | 241 242**示例:** 243 244```ts 245import { BusinessError } from '@kit.BasicServicesKit'; 246function getUserHomeDirExample() { 247 try { 248 let path = Environment.getUserHomeDir(); 249 console.info(`Succeeded in getUserHomeDir, path is ${path}`); 250 } catch (err) { 251 console.error(`Failed to getUserHomeDir. Code: ${err.code}, message: ${err.message}`); 252 } 253} 254``` 255