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