• 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 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```ts
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  ```ts
42  import { BusinessError } from '@ohos.base';
43  environment.getStorageDataDir().then((path: string) => {
44      console.info("getStorageDataDir successfully, Path: " + path);
45  }).catch((err: BusinessError) => {
46      console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
47  });
48  ```
49
50## environment.getStorageDataDir
51
52getStorageDataDir(callback:AsyncCallback<string>):void
53
54Obtains the root directory of the storage. This API uses an asynchronous callback to return the result.
55
56**System capability**: SystemCapability.FileManagement.File.Environment
57
58**Parameters**
59
60| Name  | Type                       | Mandatory| Description                            |
61| -------- | --------------------------- | ---- | -------------------------------- |
62| callback | AsyncCallback<string> | Yes  | Asynchronous callback invoked to return the root directory of the storage.|
63
64**Error codes**
65
66For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
67| ID                    | Error Message       |
68| ---------------------------- | ---------- |
69| 202 | The caller is not a system application |
70| 13900020 | Invalid argument |
71| 13900042 | Unknown error |
72
73**Example**
74
75  ```ts
76  import { BusinessError } from '@ohos.base';
77  environment.getStorageDataDir((err: BusinessError, path: string) => {
78    if (err) {
79      console.info("getStorageDataDir failed with error message: " + err.message + ", error code: " + err.code);
80    } else {
81      console.info("getStorageDataDir successfully, Path: " + path);
82    }
83  });
84  ```
85
86## environment.getUserDataDir
87
88getUserDataDir():Promise<string>
89
90Obtains the root directory of user files. This API uses a promise to return the result.
91
92**System capability**: SystemCapability.FileManagement.File.Environment
93
94**Return value**
95
96| Type                 | Description              |
97| --------------------- | ------------------ |
98| Promise<string> | Promise used to return the root directory of user files.|
99
100**Error codes**
101
102For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
103| ID                    | Error Message       |
104| ---------------------------- | ---------- |
105| 202 | The caller is not a system application |
106| 13900020 | Invalid argument |
107| 13900042 | Unknown error |
108
109**Example**
110
111  ```ts
112  import { BusinessError } from '@ohos.base';
113  environment.getUserDataDir().then((path: string) => {
114    console.info("getUserDataDir successfully, Path: " + path);
115  }).catch((err: BusinessError) => {
116    console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
117  });
118  ```
119
120## environment.getUserDataDir
121
122getUserDataDir(callback:AsyncCallback<string>): void
123
124Obtains the root directory of user files. This API uses an asynchronous callback to return the result.
125
126**System capability**: SystemCapability.FileManagement.File.Environment
127
128**Parameters**
129
130| Name  | Type                       | Mandatory| Description                            |
131| -------- | --------------------------- | ---- | -------------------------------- |
132| callback | AsyncCallback<string> | Yes  | Asynchronous callback used to return the root directory of user files.|
133
134**Error codes**
135
136For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
137| ID                    | Error Message       |
138| ---------------------------- | ---------- |
139| 202 | The caller is not a system application |
140| 13900020 | Invalid argument |
141| 13900042 | Unknown error |
142
143**Example**
144
145  ```ts
146  import { BusinessError } from '@ohos.base';
147  environment.getUserDataDir((err: BusinessError, path: string) => {
148    if (err) {
149      console.info("getUserDataDir failed with error message: " + err.message + ", error code: " + err.code);
150    } else {
151      console.info("getUserDataDir successfully, Path: " + path);
152    }
153  });
154  ```
155