When performing file operation, you can use the REMOVE () function in the CFILE class to delete a file, but such an operation will permanently delete the file, can't restore this file when necessary, solve the only way to solve this problem is Send the file to the recycle bin in the Windows system, not simple permanent deletion, so that users can restore this file when necessary.
.
This example will explain how to implement it.
Program
To implement file access operations in Windows Recycle Bank
.
I. Implementing the method in Windows's shellapi file defines a shell function called SHFileOperation (), using it to implement various file operations, such as copy, deletion, movement, etc., this function is very simple, it only has Parameters pointing to the SHFileOpstruct structure
.
When you use the shfileOperation () function, just fill in the dedicated structure - SHFileOpstruct, telling Windows to do what kind of operation, and other important information.
.
SHFileOperation () is particularly in that it is an advanced shell function, different from low-level file processing.
.
When calling the SHFileOperation operation file, the corresponding enclosure copy processor (if any) is called
.
If you delete a file, SHFileOperation will put the deleted file in Recycle Bin.
.
The original shape of the shfileOperation () function is:
WINSHELLAPI INT WINAPI SHFILEOPERATION (LPSHFILEOPSTRUCT LPFILEOP);
The parameter type in the function is an LPSHFileopStruct structure, which contains various information for file operation, which is as follows:
TypeDef struct _shfileopstruct {hwnd hwnd; // message window handle; uint wfunc; // operation type LPCSTR PFROM; // Source file and path LPCSTR PTO; // target file and path fileop_flags fflags; // Operation and confirmation flag BOOL FanyOperationsaborted; // Operation Select Bits LPVOID HNAMEMAPPINGS; // File Mapping LPCSTR LPSZPROGRESSTILE; / / File Operation Schedule Window Title} SHFILEOPSTRUCT, FAR * LPSHFILEOPStructure
In this structure, hwnd is a window handle pointing to the send message. The PFROM and PTO are the source name and destination file name for file operation. It contains the path of the file, the path string of a single file, or for multiple files, must The spacing between the end of the string or the file path name, otherwise an error occurs when the program is running. In addition, both PFROMs and PTO support wildcards * and? This greatly facilitates the use of developers. For example, there are two source files or directories, then: char pfrom [] = "d: / test1 / 0d: /text.txt/0", it indicates that all files and d: disk Test directory : TEXT.TXT file on the disk is operated. "" "" "" Is the escape character in the C language in the C language, '/ 0' is NULL. WFUNC is a very important member of the structure, which represents the type of operation that the function will perform, and its value is as follows: · FO_COPY: Copy file PFROM to the specified location of the PTO. · FO_RENAME: Create the file name of the PFROM as the file name of the PTO. · FO_MOVE: Move the PFROM's file to the PTO. · FO_DELETE: Delete the file specified by PFROM. When using this function to copy, move or delete, if the time required is long, the program will automatically appear in the process of performing a modified dialog (the file operation dialog provided by the Windows operating system), used to display Perform progress and execution time, as well as file names being copied, moved or deleted, and members lpszprogressTitle in the structure show the title of this dialog. Fflags is the process and status control identifier when performing file operation. It mainly has some of the following identifications, or it can be combined: · FOF_FILESONLY: Perform a wildcard, only perform files; · FOF_ALLOWUNDO: Save UNDO information to recover files in the recycle bin; · FOF_noconfirmation: When the target file is existing, If this item does not set, it will confirm whether the dialog is overwritten, set this, automatically confirm, overwritten, no dialog box. · FOF_NOERRORUI: After setting this item, when an error occurs during the file processing, the error prompt does not appear, otherwise an error message will be performed.
· FOF_RENAMEONCOLLISION: When there is a file name, it is replaced. · FOF_SILENT: Does not display the progress dialog. · FOF_WANTMAPPINGHANDLE: Requires the SHFILEOPERATION () function Returns the list of actual files that are in operation status, and the file list list is saved in the HNameMAppings member. The SHFileOpstruct structure also contains an array of shNamemapping structures that saves the newly old path for each operational stateful file computed by the shell. When using this function to delete a file, you must set the mystery FOF_ALLOWUNDO flag in the SHFileopStruct structure to copy the files to be deleted to Recycle Bin so that the user can revoke the delete operation. It should be noted that if the PFROM is set to a file name, remove this file with the FO_DELETE flag does not move it to Recycle bin, and even set the FOF_ALLOWUNDO flag, here you have to use the full path name, so ShfileOperation will not Move the deleted file to Recycle Bin. Second, programming step 1. Start Visual C 6.0, generate a single document view project fileDelete; 2, add a menu ID_FILEETE to the project, then use Visual C Class Wizard to view the new message processing function onfileeete in the view class ( 3, add code, compile running procedure; three, program code ///////////////////00s of: / vb / 0 "; // Source file path; STRDST [] = "D: / VB1 / 0"; // Target file path; char startle [] = "file copy"; // file delete progress dialog SHFILEOPSTRUCT fileop; // Define SHFileOpstruct structure object; fileop.hwnd = this-> m_hwnd; fileop.wfunc = fo_delete; // execute file delete operation; fileop.pfrom = strsrc; fileop.pto = strdst; fileop.fflags = fof_allowundo; // This flag makes the delete file back up to Windows Recycle Bin Fileop .hnamemappings = null; fileop.lpszprogressTitle = startle; // Start Delete file nok = shfileoperation (& fileop); if (nok) Trace ("there is an error:% d / n", nok; else trace ("SHFileOperation Finished SuccessFully / N ");
Fourth, small knots in Visual C
Program
The operation of implementing files is the content that many applications should involve, and the general way to solve this problem is to directly utilize the operating member functions of the CFILE class. This method is for Visual C .
Program
The enthusiasts are already familiar, in fact, using the methods described above, using Win32 shells to achieve copy, rename, mobile, delete and other file operations will be more efficient, fast, it is worth mentioning that this method is not only above Function, it also supports a directory or directory tree
.
At the same time, the method calls Windows directly.
operating system
The outer casing, its processing process is consistent with the Windows's own file processing process, which greatly facilitates our application and operating system.
Maintain a high degree of consistency
.