harmony 鸿蒙Obtaining the User Directory Environment (C/C++)
Obtaining the User Directory Environment (C/C++)
When to Use
You can use Environment to allow a third-party application to access files in a user directory.
Constraints
- Before using the APIs of the Environment module, ensure that the device has SystemCapability.FileManagement.File.Environment.FolderObtain.
- 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.
Available APIs
For details about the APIs, see Environment.
API | Description |
---|---|
FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result) | Obtains the sandbox path of the Download directory. This function supports only 2-in-1 devices. |
FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result) | Obtains the sandbox path of the Desktop directory. This function supports only 2-in-1 devices. |
FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result) | Obtains the sandbox path of the Documents directory. This function supports only 2-in-1 devices. |
How to Develop
Adding the Dynamic Link Library
Add the following library to CMakeLists.txt.
target_link_libraries(sample PUBLIC libohenvironment.so)
Adding Header Files
#include <cstdio>
#include <cstdlib>
#include <filemanagement/environment/oh_environment.h>
#include <filemanagement/fileio/oh_fileio.h>
Call OH_Environment_GetUserDownloadDir to obtain the sandbox path of the user Download directory. The memory allocated must be released using free().
Example:void GetUserDownloadDirPathExample() { char *downloadPath = NULL; FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); if (ret == 0) { printf("Download Path=%s", downloadPath); free(downloadPath); } else { printf("GetDownloadPath failed, error code is %d", ret); } }
Call OH_Environment_GetUserDesktopDir to obtain the sandbox path of the user Desktop directory. The memory allocated must be released using free().
Example:void GetUserDesktopDirPathExample() { char *desktopPath = NULL; FileManagement_ErrCode ret = OH_Environment_GetUserDesktopDir(&desktopPath); if (ret == 0) { printf("Desktop Path=%s", desktopPath); free(desktopPath); } else { printf("GetDesktopPath failed, error code is %d", ret); } }
Call OH_Environment_GetUserDocumentDir to obtain the sandbox path of the user Document directory. The memory allocated must be released using free().
Example:void GetUserDocumentDirPathExample() { char *documentPath = NULL; FileManagement_ErrCode ret = OH_Environment_GetUserDocumentDir(&documentPath); if (ret == 0) { printf("Document Path=%s", documentPath); free(documentPath); } else { printf("GetDocumentPath failed, error code is %d", ret); } }
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Accessing Application Files (ArkTS)
harmony 鸿蒙Accessing Backup and Restore
harmony 鸿蒙Application Data Backup and Restore Overview
harmony 鸿蒙Backup and Restore Triggered by System Applications
harmony 鸿蒙Application File Overview
harmony 鸿蒙Obtaining Application and File System Space Statistics
harmony 鸿蒙Introduction to Core File Kit
harmony 鸿蒙Developing a File Manager Application (for System Applications Only)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦