• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.environment (Directory Environment Capability)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @wangke25; @gsl_1234; @wuchengjun5-->
5<!--Designer: @gsl_1234; @wangke25-->
6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang-->
7<!--Adviser: @foryourself-->
8
9The **Environment** module provides APIs for obtaining the root directories of the storage and user files.
10
11> **NOTE**
12>
13> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
14
15## Modules to Import
16
17```ts
18import { Environment } from '@kit.CoreFileKit';
19```
20
21## Environment.getUserDownloadDir
22
23getUserDownloadDir(): string
24
25Obtains the sandbox path of the pre-authorized **Download** directory. Currently, only 2-in-1 devices are supported.
26
27**System capability**: SystemCapability.FileManagement.File.Environment.FolderObtain
28
29**Return value**
30
31| Type                 | Description                 |
32| --------------------- |---------------------|
33| string | Sandbox path of the **Download** directory obtained.|
34
35**Error codes**
36
37For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
38
39| ID   | Error Message      |
40|----------| --------- |
41| 801      | Capability not supported. |
42| 13900042 | Unknown error. |
43
44**Example**
45
46```ts
47import { BusinessError } from '@kit.BasicServicesKit';
48function getUserDownloadDirExample() {
49  try {
50    let path = Environment.getUserDownloadDir();
51    console.info(`Succeeded in getUserDownloadDir, path is ${path}`);
52  } catch (err) {
53    console.error(`Failed to getUserDownloadDir. Code: ${err.code}, message: ${err.message}`);
54  }
55}
56```
57
58## Environment.getUserDesktopDir
59
60getUserDesktopDir(): string
61
62Obtains the sandbox path of the pre-authorized **Desktop** directory. Currently, only 2-in-1 devices are supported.
63
64**System capability**: SystemCapability.FileManagement.File.Environment.FolderObtain
65
66**Return value**
67
68| Type                 | Description                 |
69| --------------------- |---------------------|
70| string | Sandbox path of the **Desktop** directory obtained.|
71
72**Error codes**
73
74For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
75
76| ID   | Error Message      |
77|----------| --------- |
78| 801      | Capability not supported. |
79| 13900042 | Unknown error. |
80
81**Example**
82
83```ts
84import { BusinessError } from '@kit.BasicServicesKit';
85function getUserDesktopDirExample() {
86  try {
87    let path = Environment.getUserDesktopDir();
88    console.info(`Succeeded in getUserDesktopDir, path is ${path}`);
89  } catch (err) {
90    console.error(`Failed to getUserDesktopDir. Code: ${err.code}, message: ${err.message}`);
91  }
92}
93```
94
95## Environment.getUserDocumentDir
96
97getUserDocumentDir(): string
98
99Obtains the sandbox path of the pre-authorized **Document** directory. Currently, only 2-in-1 devices are supported.
100
101**System capability**: SystemCapability.FileManagement.File.Environment.FolderObtain
102
103**Return value**
104
105| Type                 | Description                 |
106| --------------------- |---------------------|
107| string | Sandbox path of the **Documents** directory obtained.|
108
109**Error codes**
110
111For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
112
113| ID   | Error Message      |
114|----------| --------- |
115| 801      | Capability not supported. |
116| 13900042 | Unknown error. |
117
118**Example**
119
120```ts
121import { BusinessError } from '@kit.BasicServicesKit';
122function getUserDocumentDirExample() {
123  try {
124    let path = Environment.getUserDocumentDir();
125    console.info(`Succeeded in getUserDocumentDir, path is ${path}`);
126  } catch (err) {
127    console.error(`Failed to getUserDocumentDir. Code: ${err.code}, message: ${err.message}`);
128  }
129}
130```
131