1e41f4b71Sopenharmony_ci# Obtaining and Accessing a User Directory 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Obtaining and Accessing a User Directory (ArkTS) 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can use [ohos.file.environment](../reference/apis-core-file-kit/js-apis-file-environment.md) to allow a third-party application to access files in a user directory. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci **Constraints** 8e41f4b71Sopenharmony_ci - The device must have SystemCapability.FileManagement.File.Environment.FolderObtain. 9e41f4b71Sopenharmony_ci ```ts 10e41f4b71Sopenharmony_ci if (!canIUse('SystemCapability.FileManagement.File.Environment.FolderObtain')) { 11e41f4b71Sopenharmony_ci console.error('this api is not supported on this device'); 12e41f4b71Sopenharmony_ci return; 13e41f4b71Sopenharmony_ci } 14e41f4b71Sopenharmony_ci ``` 15e41f4b71Sopenharmony_ci - The APIs for obtaining user directories do not verify the permissions for accessing the related user directories. To access the user directories, the caller must have the related permissions. If a third-party application needs to access a user directory, user authorization for the access to the **Download**, **Documents**, or **Desktop** directory is required via a dialog box. For details, see [Requesting User Authorization](../security/AccessToken/request-user-authorization.md). 16e41f4b71Sopenharmony_ci ```json 17e41f4b71Sopenharmony_ci "requestPermissions" : [ 18e41f4b71Sopenharmony_ci "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY", 19e41f4b71Sopenharmony_ci "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY", 20e41f4b71Sopenharmony_ci "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY", 21e41f4b71Sopenharmony_ci ] 22e41f4b71Sopenharmony_ci ``` 23e41f4b71Sopenharmony_ci### Example 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci1. Obtain the path to a user folder, for example, **Download**. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci ```ts 28e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 29e41f4b71Sopenharmony_ci import { Environment } from '@kit.CoreFileKit'; 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci function getUserDirExample() { 32e41f4b71Sopenharmony_ci try { 33e41f4b71Sopenharmony_ci const downloadPath = Environment.getUserDownloadDir(); 34e41f4b71Sopenharmony_ci console.info(`success to getUserDownloadDir: ${downloadPath}`); 35e41f4b71Sopenharmony_ci const documentsPath = Environment.getUserDocumentDir(); 36e41f4b71Sopenharmony_ci console.info(`success to getUserDocumentDir: ${documentsPath}`); 37e41f4b71Sopenharmony_ci const desktopPath = Environment.getUserDesktopDir(); 38e41f4b71Sopenharmony_ci console.info(`success to getUserDesktopDir: ${desktopPath}`); 39e41f4b71Sopenharmony_ci } catch (error) { 40e41f4b71Sopenharmony_ci const err: BusinessError = error as BusinessError; 41e41f4b71Sopenharmony_ci console.error(`failed to get user dir, because: ${JSON.stringify(err)}`); 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci ``` 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci2. Access files in the **Download** folder. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci ```ts 49e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 50e41f4b71Sopenharmony_ci import { Environment } from '@kit.CoreFileKit'; 51e41f4b71Sopenharmony_ci import { fileIo as fs } from '@kit.CoreFileKit'; 52e41f4b71Sopenharmony_ci import { common } from '@kit.AbilityKit'; 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci function readUserDownloadDirExample() { 55e41f4b71Sopenharmony_ci // Check whether the caller has the READ_WRITE_DOWNLOAD_DIRECTORY permission. If not, apply for the permission from the user. 56e41f4b71Sopenharmony_ci try { 57e41f4b71Sopenharmony_ci // Obtain the path to the Download folder. 58e41f4b71Sopenharmony_ci const downloadPath = Environment.getUserDownloadDir(); 59e41f4b71Sopenharmony_ci console.info(`success to getUserDownloadDir: ${downloadPath}`); 60e41f4b71Sopenharmony_ci const context = getContext() as common.UIAbilityContext; 61e41f4b71Sopenharmony_ci const dirPath = context.filesDir; 62e41f4b71Sopenharmony_ci console.info(`success to get filesDir: ${dirPath}`); 63e41f4b71Sopenharmony_ci // List the files in the Download folder and copy them to the sandbox directory. 64e41f4b71Sopenharmony_ci let fileList: string[] = fs.listFileSync(downloadPath); 65e41f4b71Sopenharmony_ci fileList.forEach((file, index) => { 66e41f4b71Sopenharmony_ci console.info(`${downloadPath} ${index}: ${file}`); 67e41f4b71Sopenharmony_ci fs.copyFileSync(`${downloadPath}/${file}`, `${dirPath}/${file}`); 68e41f4b71Sopenharmony_ci }); 69e41f4b71Sopenharmony_ci // List the files in the sandbox directory. 70e41f4b71Sopenharmony_ci fileList = fs.listFileSync(dirPath); 71e41f4b71Sopenharmony_ci fileList.forEach((file, index) => { 72e41f4b71Sopenharmony_ci console.info(`${dirPath} ${index}: ${file}`); 73e41f4b71Sopenharmony_ci }); 74e41f4b71Sopenharmony_ci } catch (error) { 75e41f4b71Sopenharmony_ci const err: BusinessError = error as BusinessError; 76e41f4b71Sopenharmony_ci console.error(`Error code: ${err.code}, message: ${err.message}`); 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci3. Save a file to the **Download** folder. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci ```ts 84e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 85e41f4b71Sopenharmony_ci import { Environment } from '@kit.CoreFileKit'; 86e41f4b71Sopenharmony_ci import { fileIo as fs } from '@kit.CoreFileKit'; 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci function writeUserDownloadDirExample() { 89e41f4b71Sopenharmony_ci // Check whether the caller has the READ_WRITE_DOWNLOAD_DIRECTORY permission. If not, apply for the permission from the user. 90e41f4b71Sopenharmony_ci try { 91e41f4b71Sopenharmony_ci // Obtain the path to the Download folder. 92e41f4b71Sopenharmony_ci const downloadPath = Environment.getUserDownloadDir(); 93e41f4b71Sopenharmony_ci console.info(`success to getUserDownloadDir: ${downloadPath}`); 94e41f4b71Sopenharmony_ci // Save temp.txt to the Download folder. 95e41f4b71Sopenharmony_ci const file = fs.openSync(`${downloadPath}/temp.txt`, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 96e41f4b71Sopenharmony_ci fs.writeSync(file.fd, 'write a message'); 97e41f4b71Sopenharmony_ci fs.closeSync(file); 98e41f4b71Sopenharmony_ci } catch (error) { 99e41f4b71Sopenharmony_ci const err: BusinessError = error as BusinessError; 100e41f4b71Sopenharmony_ci console.error(`Error code: ${err.code}, message: ${err.message}`); 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci ``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci## Obtaining and Accessing a User Directory (C/C++) 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciIn addition to ArkTS APIs, you can use C/C++ APIs to allow a third-party application to obtain and access a user directory. For details about the APIs, see [Environment](../reference/apis-core-file-kit/_environment.md). 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci **Constraints** 112e41f4b71Sopenharmony_ci - The device must have SystemCapability.FileManagement.File.Environment.FolderObtain. 113e41f4b71Sopenharmony_ci - If a third-party application needs to access a user directory, user authorization for the access to the **Download**, **Documents**, or **Desktop** directory is required. For details, see [Requesting User Authorization](../security/AccessToken/request-user-authorization.md). 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci### Available APIs 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciFor details about the APIs, see [Environment](../reference/apis-core-file-kit/_environment.md). 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci| API | Description | 120e41f4b71Sopenharmony_ci| ------------------------------------------------------------------------ | ------------------------------ | 121e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result) | Obtains the sandbox path of the **Download** folder. This API is available only for 2-in-1 devices.| 122e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result) | Obtains the sandbox path of the **Desktop** folder. This API is available only for 2-in-1 devices. | 123e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result) | Obtains the sandbox path of the **Documents** folder. This API is available only for 2-in-1 devices.| 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci### How to Develop 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Adding Dynamic Link Libraries** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciAdd the following libraries to **CMakeLists.txt**. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci```txt 132e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohenvironment.so libhilog_ndk.z.so) 133e41f4b71Sopenharmony_ci``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Adding Header Files** 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci```c++ 138e41f4b71Sopenharmony_ci#include <filemanagement/environment/oh_environment.h> 139e41f4b71Sopenharmony_ci#include <filemanagement/fileio/oh_fileio.h> 140e41f4b71Sopenharmony_ci#include <hilog/log.h> 141e41f4b71Sopenharmony_ci``` 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci- Use **OH_Environment_GetUserDownloadDir** to obtain the sandbox path of the user **Download** folder. The memory allocated by **malloc()** must be released using **free()**. <br>Example: 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci ```c++ 146e41f4b71Sopenharmony_ci void GetUserDownloadDirExample() 147e41f4b71Sopenharmony_ci { 148e41f4b71Sopenharmony_ci char *downloadPath = nullptr; 149e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); 150e41f4b71Sopenharmony_ci if (ret == 0) { 151e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "Download Path=%{public}s", downloadPath); 152e41f4b71Sopenharmony_ci free(downloadPath); 153e41f4b71Sopenharmony_ci } else { 154e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "GetDownloadPath fail, error code is %{public}d", ret); 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci ``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci- Use **OH_Environment_GetUserDownloadDir** to obtain the sandbox path of the **Download** folder and view the files in it. <br>Example: 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci ```c++ 162e41f4b71Sopenharmony_ci void ScanUserDownloadDirPathExample() 163e41f4b71Sopenharmony_ci { 164e41f4b71Sopenharmony_ci // Obtain the Download path. 165e41f4b71Sopenharmony_ci char *downloadPath = nullptr; 166e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); 167e41f4b71Sopenharmony_ci if (ret == 0) { 168e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "Download Path=%{public}s", downloadPath); 169e41f4b71Sopenharmony_ci } else { 170e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "GetDownloadPath fail, error code is %{public}d", ret); 171e41f4b71Sopenharmony_ci return; 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci // View the files in the Download folder. 174e41f4b71Sopenharmony_ci struct dirent **namelist = {nullptr}; 175e41f4b71Sopenharmony_ci int num = scandir(downloadPath, &namelist, nullptr, nullptr); 176e41f4b71Sopenharmony_ci if (num < 0) { 177e41f4b71Sopenharmony_ci free(downloadPath); 178e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "Failed to scan dir"); 179e41f4b71Sopenharmony_ci return; 180e41f4b71Sopenharmony_ci } 181e41f4b71Sopenharmony_ci for (int i = 0; i < num; i++) { 182e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "%{public}s", namelist[i]->d_name); 183e41f4b71Sopenharmony_ci } 184e41f4b71Sopenharmony_ci free(downloadPath); 185e41f4b71Sopenharmony_ci free(namelist); 186e41f4b71Sopenharmony_ci } 187e41f4b71Sopenharmony_ci ``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci- Use **OH_Environment_GetUserDownloadDir** to obtain the sandbox path of the user **Download** folder and save **temp.txt** to this folder. <br>Example: 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci ```c++ 192e41f4b71Sopenharmony_ci void WriteUserDownloadDirPathExample() 193e41f4b71Sopenharmony_ci { 194e41f4b71Sopenharmony_ci // Obtain the Download path. 195e41f4b71Sopenharmony_ci char *downloadPath = nullptr; 196e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); 197e41f4b71Sopenharmony_ci if (ret == 0) { 198e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "Download Path=%{public}s", downloadPath); 199e41f4b71Sopenharmony_ci } else { 200e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "GetDownloadPath fail, error code is %{public}d", ret); 201e41f4b71Sopenharmony_ci return; 202e41f4b71Sopenharmony_ci } 203e41f4b71Sopenharmony_ci // Save a file to the Download folder. 204e41f4b71Sopenharmony_ci std::string filePath = std::string(downloadPath) + "/temp.txt"; 205e41f4b71Sopenharmony_ci free(downloadPath); 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci std::ofstream outfile; 208e41f4b71Sopenharmony_ci outfile.open(filePath.c_str()); 209e41f4b71Sopenharmony_ci if (!outfile) { 210e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "Failed to open file"); 211e41f4b71Sopenharmony_ci return; 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci std::string msg = "Write a message"; 214e41f4b71Sopenharmony_ci outfile.write(msg.c_str(), sizeof(msg)); 215e41f4b71Sopenharmony_ci outfile.close(); 216e41f4b71Sopenharmony_ci } 217e41f4b71Sopenharmony_ci ``` 218