There are two main ways to implement file operations in Windows. One is to use the Operation member function of the CFile class directly, it uses the low-level operation mode of the file operation, such as read, write, Getlenth and so on, and directly manipulate the attributes of the file, such as creating, reading, writing and so on. The settings for the buffer are different for the size of the file and for different computers, especially for backups of the very large database, which is difficult to achieve best results.
Another effective way is to use the Win32 shell to implement these operations for the file. It can include copying, renaming, moving, and deleting files, and can support wildcard characters such as * and? , or you can manipulate a directory or directory tree directly.
This paper analyzes the principle of Win32 Shell API for file operation, and uses visual c++6.0 to implement copy operation of files.
I. PRINCIPLE and structure
WINDOWS95/NT provides an API function SHFileOperation (), which has only one parameter pointing to the SHFILEOPSTRUCT structure. The prototype of the SHFileOperation () function is as follows:
WIN SHELL API int WINAPI SHFileOperation (lpshfileopstruct lpfileop);
The LPSHFILEOPSTRUCT structure contains a variety of information about file operations, and its specific structure is as follows:
Typedef struct _ShFILEOPSTRUCT {
HWND hWnd; //消息窗口
UINT wFunc; //操作类型
LPCSTR pFrom; //源文件及路径
LPCSTR pTo; //目标文件及路径
FILEOP_FLAGS fFlags; //操作与确认标志
BOOL fAnyOperationsAborted; //操作选择位
LPVOID hNameMappings; //文件映射
LPCSTR lpszProgressTitle; //进度窗口标题
} SHFILEOPSTRUCT, FAR* LPSHFILEOPSTRUCT;
In this structure, several members are important. An HWND is a window to send a message, and PFROM and PTO are the source and destination file names for file operations, which contain the path of the file, null as an interval between multiple filenames, and wildcard * and?. If the source file or directory has two, it should be: