Use VC ++ to access files in the Windows Recycle Bin

Source: Internet
Author: User
Tags file copy

 

Use VC ++ to access files in the Windows Recycle Bin

During file operations, you can use the remove () function in the cfile class to delete an object. However, such operations will permanently delete the object, the file cannot be restored when necessary. The only way to solve this problem is to send the file to the recycle bin (Recycle
BIN), instead of simply permanently deleting it, so that the user can restore the file as necessary. This example shows how to program the file access operation of the Windows recycle bin.

I. Implementation Method
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. For example, when deleting a file, shfileoperation places the deleted file in recycle.
Bin. The shfileoperation () function is prototype:

Winshellapi int winapi shfileoperation (lpshfileopstructlpfileop );

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: charpfrom [] = "D: \ test1 \ 0d: \ text.txt \ 0", which indicates to D: all the files in the disk test directory and the text.txt files on the disk. "\" In the string is the Escape Character of '\' in C language, 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 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 recycle.
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 mark will not move it to recycle.
Bin, or even set the fof_allowundo flag. here you must use the full path name so that shfileoperation can move the deleted file to the recycle.
Bin.

Ii. programming steps

1. Start visual c ++ 6.0 to generate a single-document view of the Project filedelete;

2. Add a menu id_filedelete for the project, and then use the class of Visual C ++
Wizard adds the onfiledelete () message processing function to the View class ();

3. Add code and compile and run the program;

3. program code

//////////////////////////////////////// ///////////////////////////////
Void cfileoperationview: onfiledelete ()
{
Int NOK;
Char strsrc [] = "D: \ VB \ 0"; // source file path;

Char strdst [] = "D: \ VB1 \ 0"; // target file path;

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 ("there is an error: % d \ n", NOK );
Else
Trace ("shfileoperation finishedsuccessfully \ n ");

}

Iv. Summary

In Visual C ++ programming, file operations are required by many applications. The general method to solve this problem is to directly use cfile.
This method is familiar to many visual c ++ programmers. In fact, we use the method described above, using the Win32 shell to copy, rename, move, delete, and other file operations will be more efficient and fast. It is worth mentioning that this method not only has the above functions, it also directly supports operations on a directory or directory tree. At the same time, this method directly calls the shell in the Windows operating system. Its processing process is consistent with that of Windows's own file processing process, this greatly benefits us to maintain a high degree of consistency between our developed applications and the operating system.

 

 

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.