Programming Method for copying files in Win95

Source: Internet
Author: User

Programming Method for copying files in Win95

Computer Room of Hubei Institute of Technology Library
Zhang Mingwu

---- There are two main methods to implement file operations in windows. One is to directly use the cfile class operation member functions. It uses the underlying operation methods for file operations, such as read, write, getlenth, and so on.CompositionFile attributes, such as creation, reading, and writing. Besides, the buffer settings vary with the file size and computers, especially for databases with a large length, it is difficult to achieve the best results.

---- Another effective method is to use the Win32 shell to perform these operations on files. It supports copying, renaming, moving, and deleting objects, and supports wildcards (such as * And ?), You can also directly operate on a directory or directory tree.

---- This article analyzes the working principle of Win32 shell API for file operations, and uses Visual C ++ 6.0 to copy files.

----I. principles and structure

---- In Windows 95/NT, an API function shfileoperation () is provided. It has only one parameter pointing to the shfileopstruct structure. The shfileoperation () function has the following prototype:

---- Win Shell API int winapi shfileoperation (lpshfileopstruct lpfileop );

---- The lpshfileopstruct structure contains various information about file operations. The specific structure is as follows:

Typedef struct _ shfileopstruct {hwnd; // message window uint wfunc; // operation type: lpcstr pfrom; // source file and Path: lpcstr PTO; // target file and Path: fileop_flags fflags; // operation and confirmation mark bool fanyoperationsaborted; // lpvoid hnamemappings; // file ing lpcstr lpszprogresstitle; // progress window title} shfileopstruct, far * lpshfileopstruct;

---- Several members are important in this structure. Hwnd refers to the window for sending messages. pfrom and PTO are the source file name and target file name for file operations. It contains the file path and uses null as the interval between multiple file names, wildcard characters * And? are supported ?. If there are two source files or directories, it should be:

 
Char pfrom [] = "C: // windows // command/0C: // dos // himem. sys/0"

---- It indicates that all files and C:/DOS/himem. sys files under the C:/Windows/command directory are operated. '//' Is the Escape Character of '/' in C language, and '/0' is null. Wfunc is an important member in the structure. It indicates the type of the operation to be performed, which is one of the following operation types:

---- Fo_copy: copy the pfrom file to the specified position of PTO.

---- Fo_rename: Rename the pfrom file name to the PTO file name.

---- Fo_move: Move the pfrom file to the PTO location.

---- Fo_delete: delete the file specified by pfrom.

---- If it takes a long time to copy, move, or delete a file, a dialog box without mode appears during the process, the execution progress, execution time, and file name that is being copied and moved or deleted can be displayed. The member lpszprogresstitle displays the title of this dialog box. Fflags is the process and status control identifier for file operations. It mainly has the following identifiers or combinations.

---- Fof_filesonly: Only files are executed without wildcards.

---- Fof_allowundo: Save the Undo information for recovery.

---- Fof_noconfirmation.

---- Fof_noerrorui: After this option is set, when an error occurs during file processing, no error prompt is displayed. Otherwise, an error prompt is displayed.

---- Fof_renameoncollision: if a file name already exists, the system prompts you to change the file name.

---- Fof_silent: The progress dialog box is not displayed.

---- Fof_wantmappinghandle: The shfileoperation () function is required to return the list of actual files in the active state. The file list name handle is stored in the hnamemappings member. Shfileopstruct will contain an array of shnamemapping structures, which stores the new and old paths of each file in the operation state calculated by shell.

----Ii. instance operations

---- This article is about visual C ++ProgramTo copy files. First, create a single file interface filecopy, and then add a new tool bar variable m_wndmytoolbar in the main framework, create a tool bar idr_mytoolbar, set a tool message id_filecopy, and in the main framework mainfrm. add the toolbar to the oncreate () member function of CPP.

 
If (! M_wndmytoolbar.create (this) |! M_wndmytoolbar.loadtoolbar (idr_mytoolbar) {trace ("can not create the filetoolbar! /N "); Return-1 ;}

---- Use the "idr_mytool" tool bar of the central visual operation tool and use classwizard to add the onfilecopy function for the message id_filecopy. Then add the following program to the processing function.

Void onfilecopy () {int NOK; char strsrc [] = "C: // DOS/0C: // pwin98 // command/0 "; // you can change the Source Path char strdst [] = "C: // temp/0"; // set the destination path char strtitle [] = "file copying "; // progress header shfileopstruct fileop; fileop. hwnd = m_hwnd; fileop. wfunc = fo_copy; // execute the file copy fileop. pfrom = strsrc; fileop. PTO = strdst; fileop. fflags = fof_allowundo; fileop. hnamemappings = NULL; fileop. lpszprogresstitle = strtitle; NOK = shfileoperation (& fileop); If (n OK) trace ("there is an error: % d/N", NOK); else trace ("shfileoperation finished successfully/N"); If (fileop. fanyoperationsaborted) trace ("operation was aborted! /N ");}

----Iii. Conclusion

---- Use the Windows API to design program file operations. It directly calls the shell in the Windows operating system, the processing process is consistent with the processing process in Windows95/98/NT, which helps us maintain consistency with the operating system in the system program design. At the same time, in File Processing, it directly calls Windows API functions, so it does not need the support of dynamic link library DLL of other applications.

---- Various file operations are provided in various development software, but they must take advantage of the deep knowledge of the file system, in addition, developers need to further design the intuition of their operations. Therefore, using the Operating System Shell for program design is a good design method.

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.