1e41f4b71Sopenharmony_ci# Obtaining the User Directory Environment (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciBefore accessing or operating a user file, a third-party application needs to obtain the user directory. The **Environment** module provides the APIs for obtaining the user directories. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Constraints 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci- Before using the APIs of the **Environment** module, ensure that the device has SystemCapability.FileManagement.File.Environment.FolderObtain. 10e41f4b71Sopenharmony_ci- 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). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Available APIs 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciFor details about the APIs, see [Environment](../reference/apis-core-file-kit/_environment.md). 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci| API| Description| 17e41f4b71Sopenharmony_ci| -------- | -------- | 18e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result)| Obtains the sandbox path of the **Download** directory. This API is available only for 2-in-1 devices.| 19e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result) | Obtains the sandbox path of the **Desktop** directory. This API is available only for 2-in-1 devices.| 20e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result) | Obtains the sandbox path of the **Documents** directory. This API is available only for 2-in-1 devices.| 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## How to Develop 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**Adding the Dynamic Link Library** 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciAdd the following library to **CMakeLists.txt**. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci```txt 29e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohenvironment.so) 30e41f4b71Sopenharmony_ci``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Adding Header Files** 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci```c++ 35e41f4b71Sopenharmony_ci#include <filemanagement/environment/oh_environment.h> 36e41f4b71Sopenharmony_ci#include <filemanagement/fileio/oh_fileio.h> 37e41f4b71Sopenharmony_ci``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci1. Use **OH_Environment_GetUserDownloadDir** to obtain the sandbox path of the user **Download** directory. The memory allocated must be released using **free()**. <br>Example: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```c 42e41f4b71Sopenharmony_ci void GetUserDownloadDirPathExample() { 43e41f4b71Sopenharmony_ci char *downloadPath = NULL; 44e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); 45e41f4b71Sopenharmony_ci if (ret == 0) { 46e41f4b71Sopenharmony_ci printf("Download Path=%s", downloadPath); 47e41f4b71Sopenharmony_ci free(downloadPath); 48e41f4b71Sopenharmony_ci } else { 49e41f4b71Sopenharmony_ci printf("GetDownloadPath failed, error code is %d", ret); 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci ``` 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci2. Use **OH_Environment_GetUserDesktopDir** to obtain the sandbox path of the user **Desktop** directory. The memory allocated must be released using **free()**. <br>Example: 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci ```c 57e41f4b71Sopenharmony_ci void GetUserDesktopDirPathExample() { 58e41f4b71Sopenharmony_ci char *desktopPath = NULL; 59e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDesktopDir(&desktopPath); 60e41f4b71Sopenharmony_ci if (ret == 0) { 61e41f4b71Sopenharmony_ci printf("Desktop Path=%s", desktopPath); 62e41f4b71Sopenharmony_ci free(desktopPath); 63e41f4b71Sopenharmony_ci } else { 64e41f4b71Sopenharmony_ci printf("GetDesktopPath failed, error code is %d", ret); 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci ``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci3. Use **OH_Environment_GetUserDocumentDir** to obtain the sandbox path of the user **Documents** directory. The memory allocated must be released using **free()**. <br>Example: 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci ```c 72e41f4b71Sopenharmony_ci void GetUserDocumentDirPathExample() { 73e41f4b71Sopenharmony_ci char *documentPath = NULL; 74e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDocumentDir(&documentPath); 75e41f4b71Sopenharmony_ci if (ret == 0) { 76e41f4b71Sopenharmony_ci printf("Document Path=%s", documentPath); 77e41f4b71Sopenharmony_ci free(documentPath); 78e41f4b71Sopenharmony_ci } else { 79e41f4b71Sopenharmony_ci printf("GetDocumentPath failed, error code is %d", ret); 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci ``` 83