Msdn上的资料:
SHFileOperation Function
Copies, moves, renames, or deletes a file system object.
Syntax
int SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
Parameters
lpFileOp
[in] Pointer to an SHFILEOPSTRUCT structure that contains information this function needs to carry out the specified operation. This parameter must contain a valid value that is not NULL. You are responsibile for validating the value. If you do not validate it, you will experience unexpected results.
Return Value
Returns zero if successful, or nonzero otherwise.
SHFILEOPSTRUCT Structure
Contains information that the SHFileOperation function uses to perform file operations.
Syntax
typedef struct _SHFILEOPSTRUCT {
HWND hwnd;
UINT wFunc;
LPCTSTR pFrom;
LPCTSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCTSTR lpszProgressTitle;
} SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
Members
hwnd
Window handle to the dialog box to display information about the status of the file operation.
wFunc
Value that indicates which operation to perform. This member can be one of the following values.
FO_COPY
Copy the files specified in the pFrom member to the location specified in the pTo member.
FO_DELETE
Delete the files specified in pFrom.
FO_MOVE
Move the files specified in pFrom to the location specified in pTo.
FO_RENAME
Rename the file specified in pFrom. You cannot use this flag to rename multiple files with a single function call. Use FO_MOVE instead.
pFrom
Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom.
pTo
Address of a buffer to contain the name of the destination file or directory. This parameter must be set to NULL if it is not used. Like pFrom, the pTomember is also a double-null terminated string and is handled in much the same way. However, pTo must meet the following specifications.
· Wildcard characters are not supported.
· Copy and Move operations can specify destination directories that do not exist and the system will attempt to create them. The system normally displays a dialog box to ask the user if they want to create the new directory. To suppress this dialog box and have the directories created silently, set the FOF_NOCONFIRMMKDIR flag in fFlags.
· For Copy and Move operations, the buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES.
· Pack multiple names into the string in the same way as for pFrom.
· Use only fully-qualified paths. Using relative paths will have unpredictable results.
fFlags
Flags that control the file operation. This member can take a combination of the following flags.
FOF_ALLOWUNDO
Preserve undo information, if possible. Operations can be undone only from the same process that performed the original operation. If pFrom does not contain fully qualified path and file names, this flag is ignored.
FOF_CONFIRMMOUSE
Not used.
FOF_FILESONLY
Perform the operation on files only if a wildcard file name (*.*) is specified.
FOF_MULTIDESTFILES
The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
FOF_NOCONFIRMATION
Respond with "Yes to All" for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR
Do not confirm the creation of a new directory if the operation requires one to be created.
FOF_NO_CONNECTED_ELEMENTS
Version 5.0. Do not move connected files as a group. Only move the specified files.
FOF_NOCOPYSECURITYATTRIBS
Version 4.71. Do not copy the security attributes of the file.
FOF_NOERRORUI
Do not display a user interface if an error occurs.
FOF_NORECURSION
Only operate in the local directory. Don't operate recursively into subdirectories.
FOF_NORECURSEREPARSE
Treat reparse points as objects, not containers. You must set _WIN32_WINNT to 5.01 or later to use this flag. See Shell and Common Controls Versions for further discussion of versioning.
FOF_RENAMEONCOLLISION
Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
FOF_SILENT
Do not display a progress dialog box.
FOF_SIMPLEPROGRESS
Display a progress dialog box but do not show the file names.
FOF_WANTMAPPINGHANDLE
If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object containing their old and new names to thehNameMappings member.
FOF_WANTNUKEWARNING
Version 5.0. Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overridesFOF_NOCONFIRMATION.
fAnyOperationsAborted
Value that receives TRUE if the user aborted any file operations before they were completed, or FALSE otherwise.
hNameMappings
A handle to a name mapping object containing the old and new names of the renamed files. This member is used only if the fFlags member includes theFOF_WANTMAPPINGHANDLE flag. See Remarks for more details.
lpszProgressTitle
Address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag.
自己做的一些例子:
#include "windows.h"
#include "winnt.h"
#include "shellapi.h"
包含上面头文件
bool CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
TCHAR ToBuf[MAX_PATH];
TCHAR FromBuf[MAX_PATH];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(FromBuf, lpszFromPath);
lstrcpy(ToBuf, lpszToPath);
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = lpszFromPath;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_COPY;
return SHFileOperation(&FileOp) == 0;
}
bool DeleteFolder(LPCTSTR lpszPath)
{
SHFILEOPSTRUCT Op;
ZeroMemory(&Op, sizeof(Op));
TCHAR ToBuf[MAX_PATH];
TCHAR FromBuf[MAX_PATH];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(FromBuf, lpszPath);
Op.hwnd = NULL;
Op.pFrom = FromBuf;
Op.pTo = ToBuf;
Op.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
Op.fAnyOperationsAborted = FALSE;
Op.hNameMappings = NULL;
Op.lpszProgressTitle = NULL;
Op.wFunc = FO_DELETE;
return SHFileOperation(&Op)==0;
}
发现这个的确很好用,既支持文件夹又支持文件,,,有个小问题,好像单个文件不能超4个G,一点点小遗憾!