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