VC + + to realize the file access of the Recycle Bin

Source: Internet
Author: User

You can use the Remove () function in the CFile class to delete a file while you are doing a file operation, but such an operation will permanently delete the file, and you cannot recover it when necessary. The only way to solve this problem is to send the file to the Recycle Bin (recycle Bin) in the Windows system, rather than simply permanently delete it, so that the user can recover the file when necessary. This example shows how to implement programming to implement file access operations for Windows Recycle Bin.

One, the realization method

A shell function named SHFileOperation () is defined in the Windows Shellapi file to enable various file operations, such as copying, deleting, moving, and so on, which is very simple to use. It has only one parameter that points to the SHFILEOPSTRUCT structure. When you use the SHFileOperation () function, just fill out the dedicated structure--shfileopstruct and tell Windows what to do and other important information. The special thing about SHFileOperation () is that it is an advanced shell function, different from low-level file processing. When the SHFileOperation operation file is invoked, the corresponding Shell copy processor (if any) is invoked. If you delete a file, SHFileOperation puts the deleted file in the Recycle Bin. The prototype of the SHFileOperation () function is:

Winshellapi int WINAPI SHFileOperation (lpshfileopstruct lpfileop);

The parameter type in the function is a LPSHFILEOPSTRUCT structure that contains various information about the file operation, and its specific structure is as follows:

Typedef struct _ShFILEOPSTRUCT
{
HWND hWnd; //消息发送的窗口句柄;
UINT wFunc; //操作类型
LPCSTR pFrom; //源文件及路径
LPCSTR pTo; //目标文件及路径
FILEOP_FLAGS fFlags; //操作与确认标志
BOOL fAnyOperationsAborted; //操作选择位
LPVOID hNameMappings; //文件映射
LPCSTR lpszProgressTitle; //文件操作进度窗口标题
}SHFILEOPSTRUCT, FAR * LPSHFILEOPSTRUCT;

In this structure, the HWND is a window handle to the sending message, PFROM and PTO are the source and destination file names for the file operation, which contains the path to the file, the path string for a single file, or, for multiple files, the interval between the end of the string or the file path name, Otherwise, an error occurs while the program is running. In addition, PFROM and PTO both support wildcard * and? , which greatly facilitates the use of developers. For example, if the source file or directory has two, it should be: Char pfrom[]= "D:\test1\0d:\text.txt\0", which represents the operation of all files in the directory of the D: Disk test and the Text.txt file on the D: disk. The "" In the string is an escape character in the C language, and the ' yes ' is null. Wfunc is a very important member of the structure that represents the type of action the function will take, and its value is as follows:

。 Fo_copy: Copies the file pfrom to the PTO specified location.

。 Fo_rename: Rename the file name of the Pfrom to PTO.

。 Fo_move: Move the Pfrom file to the PTO place.

。 Fo_delete: Deletes the file specified by Pfrom.

When you use this function to copy, move, or delete files, if it takes a long time, the program automatically appears in the process of a modeless dialog box (the File Action dialog provided by the Windows operating system) to show the progress of execution and the time of execution, as well as the name of the file being copied, moved, or deleted. The member Lpszprogresstitle in the structure now displays the title of this dialog box. Fflags is the process and status control identity when doing file operations. It mainly has some of the following identification, can also be its combination:

。 Fof_filesonly: Execute wildcard character, only execute file;

。 Fof_allowundo: Saves the undo information to restore the file in the Recycle Bin;

。 Fof_noconfirmation: When the target file already exists, if this item is not set, it will appear to confirm whether the Overwrite dialog box, set this item automatically confirm, overwrite, do not appear dialog box.

。 Fof_noerrorui: When this item is set, Error prompts do not appear when there is an error in the file processing, otherwise the error is prompted.

。 Fof_renameoncollision: When the filename is already present, replace it with a text prompt.

。 Fof_silent: The progress dialog box is not displayed.

。 Fof_wantmappinghandle: the SHFileOperation () function is required to return the list of actual files that are in operation State, and the file list name handle is saved in the Hnamemappings member.

。 The SHFILEOPSTRUCT structure also contains an array of shnamemapping structures that hold the old and new paths for each file that is in the operating state computed by the shell.

When you delete a file using this function, you must set the mysterious Fof_allowundo flag in the SHFILEOPSTRUCT structure so that you can copy the file you want to delete to the recycle Bin, allowing the user to undo the delete operation. Note that if Pfrom is set to a filename, removing the file with the FO_DELETE flag will not move it to recycle Bin, or even set the Fof_allowundo flag, where you must use the full path name, This will shfileoperation remove the deleted files to recycle Bin.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.