Programming method for file copy in Win95

zhaozj2021-02-11  233

Programming method for file copy in Win95

Hubei University of Technology Library Computer Room Zhang Mingwu

---- There are two main methods to implement files in Windows. One is to implement the operation member function of the CFILE class, it is to be used to use the underlying operation of the file operation, such as read, write, getlenth, etc., and to directly operate the file properties, such as creating, reading, writing, etc .; Moreover, the setting of its buffer is different for file size and different computers, especially for databases that are large, it is difficult to achieve optimal results.

---- Another effective way is to use Win32 housing to implement these for file operations. It can implement copy, rename, movement, and deletion of files, and can support wildcards (such as * and?), Or directly operate directly to a directory or directory tree.

---- This paper analyzes the principle of Win32 shell API for file operations and uses Visual C 6.0 to implement file copy operation.

---- One, principle and structure

---- WINDOWS95 / NT provides an API function shFileOperation (), which only has a parameter 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 various information for file operation, which is as follows:

Typedef struct _shfileopstruct {

HWND HWND; // Message Window

UINT WFUNC; // Operation Type

LPCSTR PFROM; // Source File and Path

LPCSTR PTO; / / Target Files and Paths

Fileop_flags fflags; // Operation and confirmation flag

Bool fanyopertySaborted; // Operation selection bit

LPVOID HNAMEMAPPINGS; / / file mapping

LPCSTR LPSZPROGRESSTILE; / / Schedule window title

SHFILEOPSTRUCT, FAR * LPSHFILEOPStruct;

---- In this structure, there are several members important. HWnd is a window that points to the send message. The PFROM and PTO are the source name and destination file name for file operation. It contains the path to the file, with NULL as a spacing between multiple file names, and can support wildcard * and? . If there are two source files or directories, it should be:

Char pfrom [] = "c: // windows // command

/ 0c://dos//himem.sys/0 "

---- It represents all the files and C: /Dos/HIMEM.SYS files in the C: / Windows / Command directory. '//' is the '/' of the '/' in the C language, '/ 0' is NULL. WFUNC is an important member of the structure, which points to one of the types of operations that will be performed, one of the following operation types:

---- FO_COPY: Copy file PFROM to the specified location of the PTO.

---- FO_RENAME: Create the file name of the PFROM name as the file name of the PTO.

---- FO_MOVE: Move the PFROM's file to the PTO.

---- FO_DELETE: Delete the file specified by the PFROM.

---- When a file copy, move or delete, if the time required is long, a non-mode dialog box will appear during the process, which can display the progress and execution of the execution, and the positive copy movement. Or delete file name, member lpszprogressTitle displays the title of this dialog. Fflags is the process and status control identifier when performing file operation. It mainly has some of the following identifications, or it can be combined. ---- FOF_FILESONLY: Do not perform wildcards, only files.

---- FOF_ALLOWUNDO: Save Undo information for recovery.

---- FOF_NOCONFIRMATION: When the target file already exists, if this item is not set, it will confirm whether the dialog is overwritten, set this, automatically confirm, override, and no dialog box.

---- FOF_NOERRORUI: When this item is set, the error prompt does not appear when an error occurs during the file processing, otherwise an error message will be performed.

---- FOF_RENAMEONCOLLISION: When the file name already exists, the file name is replaced.

---- FOF_SILENT: The progress dialog is not displayed.

---- FOF_WANTMAPPINGHANDLE: Requires the SHFILEOPERATION () function Returns the list of actual files that are in operation status, and the file list list is saved in the HNameMAppings member. SHFileOpstruct will contain an array of shNameMApping structures that saves the new path of each operating status with the shell.

---- II, instance operation

---- This article implements an instance of file replication on a Visual C program. First create a single document interface filecopy, then add a toolbar variable m_wndmytoolbar in the main frame, create a toolbar idR_mytoolbar, set a tool message ID_FileCopy, and join the toolbar in the oncreate () member function of the main frame mainfrm.cpp .

IF (! m_wndmytoolbar.create (this) ||

M_WndmyToolBar.LoadToolbar (IDR_MYTOOLBAR))

{

Trace ("Can Not Create The Filetoolbar! / N");

Return -1;

}

---- Through the central view operation tool strip IDR_MYTOOL, use classwizard to add message processing function onfileCopy. Then add the following program in the process function.

void onfilecopy ()

{

Int nok;

Char strsrc [] = "c: // DOS / 0C: // pWIN98 // command / 0";

/ / Can change the source path

Char strdst [] = "c: // temp / 0";

/ / Set the destination path

Char strtitle [] = "File Copying";

// progress head

SHFileOpstruct fileop;

Fileop.hwnd = m_hwnd;

Fileop.wfunc = fo_copy;

// Execute a copy of the file

Fileop.pfrom = strsrc;

Fileop.pto = strdst;

Fileop.fflags = FOF_ALLOWUNDO;

Fileop.hnamemappings = null;

Fileop.lpszprogresstitle = startle;

Nok = shfileopance (& fileop);

IF (NOK)

Trace ("there is an an error:% d / n", nok); ELSE

Trace ("SHFILEOPERATION

Finished successfully / n ");

Fileop.fanyOperationsaborted

Trace ("Operation Was Aborted! / N");

}

---- Third, conclude

---- Using the Windows API for program file operation design, it calls the outer casing in the Windows operating system, and its processing procedure is consistent with the processing process in Windows95 / 98 / NT, which is conducive to our in system programming. Consistency of the operating system; at the same time, because it is directly called the Windows API function directly, there is no need for other application dynamic link library DLL support.

---- In various development software, it provides various operations for files, but it must be utilized to use a more in-depth file system, and it is also necessary for developers to further design for the intuitiveness of its operation. Therefore, it is a good design method using the operating system shell.

转载请注明原文地址:https://www.9cbs.com/read-4057.html

New Post(0)