1# @ohos.file.environment (Directory Environment Capability) 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> - The APIs of this module are system APIs and cannot be called by third-party applications. 9 10## Modules to Import 11 12```js 13import environment from '@ohos.file.environment'; 14``` 15 16## environment.getStorageDataDir 17 18getStorageDataDir():Promise<string> 19 20Obtains the root directory of the storage. This API uses a promise to return the result. 21 22**System capability**: SystemCapability.FileManagement.File.Environment 23 24**Return value** 25 26| Type | Description | 27| --------------------- | ---------------- | 28| Promise<string> | Promise used to return the root directory of the storage.| 29 30**Error codes** 31 32For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 33| ID | Error Message | 34| ---------------------------- | ---------- | 35| 202 | The caller is not a system application | 36| 13900020 | Invalid argument | 37| 13900042 | Unknown error | 38 39**Example** 40 41 ```js 42 environment.getStorageDataDir().then((path) => { 43 console.info("getStorageDataDir successfully, Path: " + path); 44 }).catch((err) => { 45 console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code); 46 }); 47 ``` 48 49## environment.getStorageDataDir 50 51getStorageDataDir(callback:AsyncCallback<string>):void 52 53Obtains the root directory of the storage. This API uses an asynchronous callback to return the result. 54 55**System capability**: SystemCapability.FileManagement.File.Environment 56 57**Parameters** 58 59| Name | Type | Mandatory| Description | 60| -------- | --------------------------- | ---- | -------------------------------- | 61| callback | AsyncCallback<string> | Yes | Asynchronous callback invoked to return the root directory of the storage.| 62 63**Error codes** 64 65For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 66| ID | Error Message | 67| ---------------------------- | ---------- | 68| 202 | The caller is not a system application | 69| 13900020 | Invalid argument | 70| 13900042 | Unknown error | 71 72**Example** 73 74 ```js 75 environment.getStorageDataDir((err, path) => { 76 if (err) { 77 console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code); 78 } else { 79 console.info("getStorageDataDir successfully, Path: " + path); 80 } 81 }); 82 ``` 83 84## environment.getUserDataDir 85 86getUserDataDir():Promise<string> 87 88Obtains the root directory of user files. This API uses a promise to return the result. 89 90**System capability**: SystemCapability.FileManagement.File.Environment 91 92**Return value** 93 94| Type | Description | 95| --------------------- | ------------------ | 96| Promise<string> | Promise used to return the root directory of user files.| 97 98**Error codes** 99 100For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 101| ID | Error Message | 102| ---------------------------- | ---------- | 103| 202 | The caller is not a system application | 104| 13900020 | Invalid argument | 105| 13900042 | Unknown error | 106 107**Example** 108 109 ```js 110 environment.getUserDataDir().then((path) => { 111 console.info("getUserDataDir successfully, Path: " + path); 112 }).catch((err) => { 113 console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code); 114 }); 115 ``` 116 117## environment.getUserDataDir 118 119getUserDataDir(callback:AsyncCallback<string>): void 120 121Obtains the root directory of user files. This API uses an asynchronous callback to return the result. 122 123**System capability**: SystemCapability.FileManagement.File.Environment 124 125**Parameters** 126 127| Name | Type | Mandatory| Description | 128| -------- | --------------------------- | ---- | -------------------------------- | 129| callback | AsyncCallback<string> | Yes | Asynchronous callback used to return the root directory of user files.| 130 131**Error codes** 132 133For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 134| ID | Error Message | 135| ---------------------------- | ---------- | 136| 202 | The caller is not a system application | 137| 13900020 | Invalid argument | 138| 13900042 | Unknown error | 139 140**Example** 141 142 ```js 143 environment.getUserDataDir((err, path) => { 144 if (err) { 145 console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code); 146 } else { 147 console.info("getUserDataDir successfully, Path: " + path); 148 } 149 }); 150 ``` 151