Put files in the recycle bin using VC

Source: Internet
Author: User
Put files in the recycle bin using VC

Address: http://www.cnblogs.com/txwsh1/archive/2008/07/11/1240775.html

 

The remove () function in the cfile class is used to delete an object. However, such an operation permanently deletes the object and cannot restore the object if necessary, the solution to this problem is to send the file to the recycle bin in the Windows system, rather than simply permanently deleting it, so that the user can restore the file as necessary.
A shell function named shfileoperation () is defined in the shellapi file of windows. It can be used to perform various file operations, such as copying, deleting, and moving objects, this function is very simple to use. It has only one parameter pointing to the shfileopstruct structure. When using the shfileoperation () function, you only need to fill in the specific structure-shfileopstruct, To tell windows what kind of operation to perform and other important information. Shfileoperation () is an advanced shell function, different from low-level file processing. When the shfileoperation operation file is called, the corresponding shell copy processor (if any) is called. If a file is deleted, shfileoperation places the deleted file in the recycle bin. The shfileoperation () function is prototype:

Winshellapi int winapi shfileoperation (lpshfileopstruct lpfileop );

The parameter type in the function is an lpshfileopstruct structure, which contains various information for file operations. The specific structure is as follows:

Typedef struct _ shfileopstruct
{
Hwnd; // handle of the message sending window;
Uint wfunc; // Operation Type
Lpcstr pfrom; // source file and Path
Lpcstr PTO; // target file and Path
Fileop_flags fflags; // operation and validation mark
Bool fanyoperationsaborted; // operation selection bit
Lpvoid hnamemappings; // file ing
Lpcstr lpszprogresstitle; // the title of the file operation progress window.
} Shfileopstruct, far * lpshfileopstruct;

In this structure, hwnd points to the window handle for sending messages, and pfrom and PTO are the source file name and target file name for file operations. It contains the file path and corresponds to the path string of a single file, or multiple files must end with null as a string or the gap between the file path names. Otherwise, an error may occur when the program is running. In addition, both pfrom and PTO support wildcards * And ?, This greatly facilitates the use of developers. For example, if there are two source files or directories, it should be: Char pfrom [] = "E: // test1/0e: // text.txt/0", which indicates: all files in the test directory of the disk and text.txt files on the E: disk. "//" In the string is the Escape Character of '/', and '/0' is null.
Wfunc is a very important member in the structure. It represents the operation type to be performed by the function. Its value is as follows:
· Fo _ copy: copy the pfrom file to the specified position of PTO.
· Fo _ rename: Rename the pfrom file name to the PTO file name.
· Fo _ move: Move the pfrom file to the PTO location.
· Fo _ Delete: delete the file specified by pfrom.

If it takes a long time to copy, move, or delete an object, the program automatically displays a non-modal dialog box (File Operation dialog box provided by the Windows operating system) to display the execution progress and execution time, and the file name that is being copied, moved, or deleted. In this case, the lpszprogresstitle member in the structure displays the title of this dialog box. Fflags is the process and status control identifier for file operations. It mainly has the following identifiers or combinations:

· Fof _ filesonly: executes wildcards and only executes files;
· Fof _ allowundo: saves undo information to restore files in the recycle bin;
· Fof _ noconfirmation: If this option is not set when the target file already exists, a dialog box is displayed to confirm whether to overwrite the file. If this option is set, the system automatically confirms and overwrites the file, the dialog box is not displayed.
· Fof _ noerrorui: After this option is set, when an error occurs during file processing, no error prompt is displayed. Otherwise, an error prompt is displayed.
· Fof _ renameoncollision: if a file name already exists, a text change is prompted.
· Fof _ silent: The progress dialog box is not displayed.
· Fof _ wantmappinghandle: The shfileoperation () function is required to return the list of actual files in the active state. The file list name handle is stored in the hnamemappings member.
· The shfileopstruct structure also contains an array of shnamemapping structures, which stores the new and old paths of each file in the operation state calculated by shell.

When using this function to delete a file, you must set the mysterious fof_allowundo flag in the shfileopstruct structure to copy the file to the recycle bin, so that you can cancel the delete operation. Note that if pfrom is set to a file name, deleting the file with the fo_delete flag will not move it to the recycle bin, or even set the fof_allowundo flag. You must use the full path name here, in this way, shfileoperation will move the deleted files to the recycle bin.

Coding implementation:
Create an MFC project in VC (single document, multi-document, and dialog box are all supported, and SDK and command line are also supported. This example is a multi-document Project)
Add an event handler for a menu item. The code in the function is as follows:

Int NOK;
Char strsrc [] = "E: // test/0"; // source file path. Add '/0'
Char strdst [] = "E: // test/0"; // target file path. Add '/0'
Char strtitle [] = "file copy"; // File Deletion progress dialog box title
Shfileopstruct fileop; // defines the shfileopstruct structure object;
Fileop. hwnd = This-> m_hwnd;
Fileop. wfunc = fo_delete; // execute the file deletion operation;
Fileop. pfrom = strsrc;
Fileop. PTO = strdst;
Fileop. fflags = fof_allowundo; // this flag backs up deleted files to the Windows recycle bin.
Fileop. hnamemappings = NULL;
Fileop. lpszprogresstitle = strtitle;
// Start deleting objects
NOK = shfileoperation (& fileop );
If (NOK)
Trace ("error: % d/N", NOK );
Else
Trace ("operation succeeded/N ");

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.