Windows api--shfileoperation--file Operations

Source: Internet
Author: User

1 int SHFileOperation (lpshfileopstruct lpfileop);

Returns 0 if execution succeeds.

typedef struct _SHFILEOPSTRUCT {HWND hwnd;//points to the window that sends the message UINT wfunc;//operation LPCTSTR pfrom;//source file name lpctstr pto;//destination file name Fileop_ FLAGS fflags;//operation and confirmation identifies whether the bool fanyoperationsaborted;//operation terminates lpvoid hnamemappings;//file map LPCTSTR lpszprogresstitle;// Progress bar Title} shfileopstruct, *lpshfileopstruct;

Parameter description:

Wfunc: the action to perform

Fo_copy: Copy fo_delete: Delete fo_move: Move fo_rename: Rename

Pfrom: Source file name

MSDN says to end with two, so the general string must also be added to a

Like what:

Incorrect usage lptstr pszsource = l "c:\\windows";//correct usage lptstr Pszsource = l "c:\\windows\0";

For CString, the correct usage is this.

CString strsource ("C:\\Windows"); strsource + = ' + ';//note must be ' \ ' instead of '


Fflags: Execute identity

Fof_silent//Does not produce a dialog box that is being copied fof_noconfirmmkdir//if the destination directory does not exist, it is created by default fof_noconfirmation//does not appear with the Confirm File Replacement dialog box (default replaces original article I pieces) fof_ noerrorui//error dialog box does not appear

Note:

Note: It is best not to use Fof_noerrorui,fof_noconfirmmkdir at the same time, because Fof_noconfirmmkdir masks missing directory Error, but fof_ NoError also blocked the missing directory Error, then the new directory is not quiet (no user confirmation to generate a new Directory dialog box) when using Fof_noerrorui,fof_noconfirmmkdir!! So how do you use Fof_noerrorui,fof_noconfirmmkdir at the same time?
You must first confirm that the directory specified by the PTO is present.

BOOL makesurediretorypathexists (LPCSTR dirpath); Use it to include Imagehlp.h and Imagehlp.lib

How to judge simultaneous existence of Fof_noerrorui,fof_noconfirmmkdir

=====

If the pop-up "cannot delete file cannot: Read source file or disk text.", you can: fof_noconfirmation| fof_noconfirmmkdir| Fof_noerrorui

=====

fof_renameoncollision//have duplicate files automatically rename Fof_allowundo     //Put files into the Recycle Bin, otherwise directly deleted, generally this is the best to do the default


When you use this function for file copy, move, or delete, if it takes a long time, the program automatically displays a modeless dialog box during the process (the File Operation dialog box provided by the Windows operating system), which shows the progress and execution time of the execution, and the name of the file being copied, moved, or deleted. The members in the structure Lpszprogresstitle display the caption for this dialog box. Fflags is the process and status control identification during file operation. It mainly has some of the following identities, or it can be a combination:

fof_filesonly//executes wildcards, executes only files, fof_allowundo//saves undo information to recover files in the Recycle Bin;

fof_noconfirmation//if the target file already exists, if you do not set this, it will appear to confirm whether to overwrite the dialog box, set this to automatically confirm, overwrite, do not appear dialog box. fof_noerrorui//When this entry is set, an error message does not appear when an error occurs during file processing, otherwise an error is prompted. fof_renameoncollision//when a file name already exists, replace it with a text prompt. fof_silent//The Progress dialog box is not displayed. Fof_wantmappinghandle


When you use this function to delete a file, you must set the mysterious Fof_allowundo flag in the SHFILEOPSTRUCT structure so that the file to be deleted is copied to the Recycle Bin, which allows the user to undo the delete operation. Note that if Pfrom is set to a filename, deleting 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 the deleted files to the Recycle Bin.
Example:

CString strsrc ("D:\\test1"); CString strdes ("D:\\test2"); Strsrc + = ' + ';//must add '/' strdes ' + + '; Shfileopstruct Fileop; Fileop.fflags = fof_allowundo| fof_multidestfiles| Fof_simpleprogress;//cstring Str ("in Progress"); str + = ' + '; Fileop.lpszprogresstitle = str; Fileop.hwnd =m_hwnd; Fileop.hnamemappings = NULL; Fileop.pfrom = STRSRC; Fileop.pto = Strdes; Fileop.wfunc = fo_copy; BOOL Bok;bok = SHFileOperation (&FILEOP); fileop.fanyoperationsaborted)//terminating {    if (bOk)        MessageBox (_t ("Operation Error! "));    else        MessageBox (_t ("Operation Complete! "));} else{    MessageBox (_t ("Operation terminated!) ");}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows api--shfileoperation--file Operations

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.