Delete a file to the recycle bin

Source: Internet
Author: User

Http://hi.baidu.com/magicfuzzx/item/cda3c631afc5cb009dc65e0e

In general, we can easily use the delatefile function to delete files. However, another drawback is that the deleted files are not deleted by the recycle bin. If they are deleted by mistake, it will be troublesome, not all users recover data. Microsoft also provides shellapi for us, where the functions can be used for some special operations.

 

Here we useShfileoperationThe function is defined as follows:

Int shfileoperation (

_ In lpshfileopstruct lpfileop

);

However, Microsoft recommends using Vista and later platforms.IfileoperationInterface to replace this function.

 

This parameter is a shfileopstruct struct, which is defined as follows:

Typedef struct _ shfileopstruct {

Hwnd; // a dialog box handle used to display the file operation status

Uint wfunc; // the action to be executed

Lpctstr pfrom; // source file path

Lpctstr PTO; // target file path

Fileop_flags fflags; // understand the additional operation options, which can be used | Operator

Bool fanyoperationsaborted; // determines whether it can be interrupted.

Lpvoid hnamemappings; // you can create a file ing.

Lpctstr lpszprogresstitle; // specify the window name when it is marked as fof_simpleprogress

} Shfileopstruct, * lpshfileopstruct;

Reference connection: http://msdn.microsoft.com/en-us/library/bb759795%28v=vs.85%29.aspx

 

The code for moving files directly to the recycle bin without confirmation is as follows:

Bool delfiletorecycle (lpctstr lpsourcefile)

{

Shfileopstruct shfile;

Zeromemory (& shfile, sizeof (shfile ));

Shfile. pfrom = lpsourcefile;

Shfile. wfunc = fo_delete;

// Silent, retain undo (Recycle Bin), cancel warning

Shfile. fflags = fof_silent | fof_allowundo | fof_noconfirmation;

If (shfileoperation (& shfile )! = 0)

{

Return false;

}

Else

{

Return true;

}

 

}

Function call is simple: delfiletorecycle (_ T ("D: \ new release document .txt "));

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.