• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.environment (Directory Environment Capability)
2
3The **Environment** module provides APIs for obtaining the root directories of the storage and public 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> - The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
10
11## Modules to Import
12
13```js
14import environment from '@ohos.file.environment';
15```
16
17## environment.getStorageDataDir
18
19getStorageDataDir():Promise<string>
20
21Obtains the root directory of the storage. This API uses a promise to return the result.
22
23**System capability**: SystemCapability.FileManagement.File.Environment
24
25**Return value**
26
27| Type                 | Description            |
28| --------------------- | ---------------- |
29| Promise<string> | Promise used to return the root directory of the storage.|
30
31**Example**
32
33  ```js
34  environment.getStorageDataDir().then((path) => {
35      console.info("getStorageDataDir successfully, Path: " + path);
36  }).catch((err) => {
37      console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
38  });
39  ```
40
41## environment.getStorageDataDir
42
43getStorageDataDir(callback:AsyncCallback<string>):void
44
45Obtains the root directory of the storage. This API uses an asynchronous callback to return the result.
46
47**System capability**: SystemCapability.FileManagement.File.Environment
48
49**Parameters**
50
51| Name  | Type                       | Mandatory| Description                            |
52| -------- | --------------------------- | ---- | -------------------------------- |
53| callback | AsyncCallback<string> | Yes  | Asynchronous callback invoked to return the root directory of the storage.|
54
55**Example**
56
57  ```js
58  environment.getStorageDataDir((err, path) => {
59    if (err) {
60      console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
61    } else {
62      console.info("getStorageDataDir successfully, Path: " + path);
63    }
64  });
65  ```
66
67## environment.getUserDataDir
68
69getUserDataDir():Promise<string>
70
71Obtains the root directory of public files. This API uses a promise to return the result.
72
73**System capability**: SystemCapability.FileManagement.File.Environment
74
75**Return value**
76
77| Type                 | Description              |
78| --------------------- | ------------------ |
79| Promise<string> | Promise returned with the root directory of public files.|
80
81**Example**
82
83  ```js
84  environment.getUserDataDir().then((path) => {
85    console.info("getUserDataDir successfully, Path: " + path);
86  }).catch((err) => {
87    console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
88  });
89  ```
90
91## environment.getUserDataDir
92
93getUserDataDir(callback:AsyncCallback<string>): void
94
95Obtains the root directory of public files. This API uses an asynchronous callback to return the result.
96
97**System capability**: SystemCapability.FileManagement.File.Environment
98
99**Parameters**
100
101| Name  | Type                       | Mandatory| Description                            |
102| -------- | --------------------------- | ---- | -------------------------------- |
103| callback | AsyncCallback<string> | Yes  | Asynchronous callback used to return the root directory of public files.|
104
105**Example**
106
107  ```js
108  environment.getUserDataDir((err, path) => {
109    if (err) {
110      console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
111    } else {
112      console.info("getUserDataDir successfully, Path: " + path);
113    }
114  });
115  ```
116