1# @ohos.file.environment (Directory Environment Capability) (System API) 2 3The **Environment** module provides APIs for obtaining the root directories of the storage and user files. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.file.environment](js-apis-file-environment-sys.md). 9 10## Modules to Import 11 12```ts 13import { Environment } from '@kit.CoreFileKit'; 14``` 15 16## environment.getStorageDataDir 17 18getStorageDataDir():Promise<string> 19 20Obtains the root directory of the memory. This API uses a promise to return the result. 21 22**System capability**: SystemCapability.FileManagement.File.Environment 23 24**System API**: This is a system API. 25 26**Return value** 27 28| Type | Description | 29| --------------------- | ---------------- | 30| Promise<string> | Promise used to return the root directory of the memory.| 31 32**Error codes** 33 34For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 35| ID | Error Message | 36| ---------------------------- | ---------- | 37| 202 | The caller is not a system application | 38| 13900020 | Invalid argument | 39| 13900042 | Unknown error | 40 41**Example** 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 56Obtains the root directory of the memory. This API uses an asynchronous callback to return the result. 57 58**System capability**: SystemCapability.FileManagement.File.Environment 59 60**System API**: This is a system API. 61 62**Parameters** 63 64| Name | Type | Mandatory| Description | 65| -------- | --------------------------- | ---- | -------------------------------- | 66| callback | AsyncCallback<string> | Yes | Callback used to return the root directory of the memory.| 67 68**Error codes** 69 70For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 71| ID | Error Message | 72| ---------------------------- | ---------- | 73| 202 | The caller is not a system application | 74| 13900020 | Invalid argument | 75| 13900042 | Unknown error | 76 77**Example** 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 94Obtains the root directory of user files. This API uses a promise to return the result. 95 96**System capability**: SystemCapability.FileManagement.File.Environment 97 98**System API**: This is a system API. 99 100**Return value** 101 102| Type | Description | 103| --------------------- | ------------------ | 104| Promise<string> | Promise used to return the root directory of user files.| 105 106**Error codes** 107 108For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 109| ID | Error Message | 110| ---------------------------- | ---------- | 111| 202 | The caller is not a system application | 112| 13900020 | Invalid argument | 113| 13900042 | Unknown error | 114 115**Example** 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 130Obtains the root directory of user files. This API uses an asynchronous callback to return the result. 131 132**System capability**: SystemCapability.FileManagement.File.Environment 133 134**System API**: This is a system API. 135 136**Parameters** 137 138| Name | Type | Mandatory| Description | 139| -------- | --------------------------- | ---- | -------------------------------- | 140| callback | AsyncCallback<string> | Yes | Callback used to return the root directory of user files.| 141 142**Error codes** 143 144For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 145| ID | Error Message | 146| ---------------------------- | ---------- | 147| 202 | The caller is not a system application | 148| 13900020 | Invalid argument | 149| 13900042 | Unknown error | 150 151**Example** 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 168Obtains the sandbox path of the root directory of an external storage card. This API is available only to the devices with the SystemCapability.FileManagement.File.Environment.FolderObtain system capability. 169 170**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 171 172**System capability**: SystemCapability.FileManagement.File.Environment.FolderObtain 173 174**System API**: This is a system API. 175 176**Return value** 177 178| Type | Description | 179| --------------------- |---------------| 180| string | Sandbox path of the root directory obtained.| 181 182**Error codes** 183 184For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 185 186| ID | Error Message | 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**Example** 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 211Obtains the sandbox path of the built-in card directory of the current user. This API is available only to the devices with the SystemCapability.FileManagement.File.Environment.FolderObtain system capability. 212 213**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 214 215**System capability**: SystemCapability.FileManagement.File.Environment.FolderObtain 216 217**System API**: This is a system API. 218 219**Return value** 220 221| Type | Description | 222| --------------------- |-----------------| 223| string | Sandbox path of the built-in card directory obtained.| 224 225**Error codes** 226 227For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 228 229| ID | Error Message | 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**Example** 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