1# Obtaining the User Directory Environment (C/C++) 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 9## When to Use 10 11You can use [Environment](../reference/apis-core-file-kit/capi-oh-environment-h.md) to allow a third-party application to access files in a user directory. 12 13## Constraints 14 15- Before using the APIs of the **Environment** module, ensure that the device has SystemCapability.FileManagement.File.Environment.FolderObtain. 16- The APIs provided by the **Environment** module can be used to obtain the application sandbox paths of the user directories. To operate the related directory and its subdirectories, user authorization is required via a dialog box. For details, see [Requesting User Authorization](../security/AccessToken/request-user-authorization.md). 17 18## Available APIs 19 20For details about the APIs, see [API Reference](../reference/apis-core-file-kit/capi-oh-environment-h.md). 21 22| API| Description| 23| -------- | -------- | 24| FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result)| Obtains the sandbox path of the **Download** directory. This function supports only 2-in-1 devices.| 25| FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result) | Obtains the sandbox path of the **Desktop** directory. This function supports only 2-in-1 devices.| 26| FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result) | Obtains the sandbox path of the **Documents** directory. This function supports only 2-in-1 devices.| 27 28## How to Develop 29 30**Adding the Dynamic Link Library** 31 32Add the following library to **CMakeLists.txt**. 33 34```txt 35target_link_libraries(sample PUBLIC libohenvironment.so) 36``` 37 38**Adding Header Files** 39 40```c++ 41#include <cstdio> 42#include <cstdlib> 43#include <filemanagement/environment/oh_environment.h> 44#include <filemanagement/fileio/oh_fileio.h> 45``` 46 471. Call **OH_Environment_GetUserDownloadDir** to obtain the sandbox path of the user **Download** directory. The memory allocated must be released using **free()**. <br>Example: 48 49 ```c 50 void GetUserDownloadDirPathExample() { 51 char *downloadPath = NULL; 52 FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); 53 if (ret == 0) { 54 printf("Download Path=%s", downloadPath); 55 free(downloadPath); 56 } else { 57 printf("GetDownloadPath failed, error code is %d", ret); 58 } 59 } 60 ``` 61 622. Call **OH_Environment_GetUserDesktopDir** to obtain the sandbox path of the user **Desktop** directory. The memory allocated must be released using **free()**. <br>Example: 63 64 ```c 65 void GetUserDesktopDirPathExample() { 66 char *desktopPath = NULL; 67 FileManagement_ErrCode ret = OH_Environment_GetUserDesktopDir(&desktopPath); 68 if (ret == 0) { 69 printf("Desktop Path=%s", desktopPath); 70 free(desktopPath); 71 } else { 72 printf("GetDesktopPath failed, error code is %d", ret); 73 } 74 } 75 ``` 76 773. Call **OH_Environment_GetUserDocumentDir** to obtain the sandbox path of the user **Document** directory. The memory allocated must be released using **free()**. <br>Example: 78 79 ```c 80 void GetUserDocumentDirPathExample() { 81 char *documentPath = NULL; 82 FileManagement_ErrCode ret = OH_Environment_GetUserDocumentDir(&documentPath); 83 if (ret == 0) { 84 printf("Document Path=%s", documentPath); 85 free(documentPath); 86 } else { 87 printf("GetDocumentPath failed, error code is %d", ret); 88 } 89 } 90 ``` 91