Windows provides an API function for files to operate and its features (reproduced)

xiaoxiao2021-04-03  210

Windows provides API functions for files and their features Alisi Web development

Author: [reprint] Source: Updated: 2006-3-9

The following is the API function and its functions provided for files provided in Windows:

Windows API function

Features

CloseHandle Close a kernel object. These include files, file mapping, processes, threads, security, and synchronous objects, and more. This function is usually similar to the VB's close command when it is related to file processing. Close should be used as much as possible because it supports VB's error control. Note that the file handle used by this function and the VB file number are completely different CompareFileTime according to the information of the FileTime structure, compare the time CopyFile file for both files. Filecopy commands with VB Similar CreateFile This is a full-featured function that opens and creates files, pipes, mail, communication services, devices, and console deletefile deletes a specified file FindClose Close a search sentence created by a search handle of a search handle Findfirstfile according to files Name Finding File FindnextFile Find the next file FLUSHFILEBUFFERS to the specified file handle according to the call of the FindFirstFile function, refresh the internal file buffer getBinaryType determines whether the product getFileinformationByHandle provides a File information. One mechanism - in a BY_HANDLE_FILE_INFORMATION structure loaded with the file associated with the file GetFileSize determines that the time information getting the specified file getting the specified file GetFileType GetFileType Gets the file type GetFileVersionInfo from the support version tag. One module Get File Version Information GetFileVersionFoSize For a file containing version resources, it is determined that the full-time file version information requires a large buffer getShortPathname Get the short-path name of the specified file. The function contains a temporary file name, which can be used by the application using LClose Turn off the specified file, please refer to the CloseHandle function to learn a further situation LCREAT to create a file. If the file already exists, it will be shortened to zero length and open it to read and write the current location of the read and write in the LLSeek setting file. This function is similar to the SEEK statement of VB. If you open a file with the vb's open command, don't use the LLSeek function LockFile to use the file in Windows, file available shared mode - In this case, multiple processes can access the file at the same time. With this function, an application to read and write files can lock a part of the file to not be accessed by other applications. This avoids the conflict LockfileEx that occurs when reading and writing, is similar to the lockfile, but it provides more features that open the specified file Lustom in binary mode to read data into the memory buffer LWRITE from memory buffers. Write a file MoveFile, MoveFileEx mobile file. If DWFLAGS is set to zero, the MoveFile is completely equivalent to the MoveFileExopenfile this function performs a large number of different file operations. Compared to this function, please give priority to Win32's CreateFile function (which opens named pipes and control Unicode file names, without restricted data of 128 characters) ReadFiles from the file. Compared with the LREAD function, this function is much flexible.

This function can operate communication devices, pipes, sockets, and postfileex readfileex, just it can only be used for asynchronous reading, and contain a complete callback searchPath lookup specified file SETENDOFFILE for an open file, will current File Location Set to File Attribute Setup SetFileAttribute Settings File Properties SetFilePointer Sets the Current Read and write position setFiletime Settings File Creation, Access, and Last Modify Time UnlockFile Unlock UnlockFileEx Unlock WriteFile to a File Writefile Write a file. This function is much flexible than the LWRITE function. This function can also be applied to the communication device, pipeline, socket, and postlock processing WriteFileEx similar to Writefile, but it can only be used for asynchronous writing, and includes a complete callback let us pass several examples. Detailed understanding of several of the main functions and their usage: 1. CreateFile effect: This is a full-featured routine that opens and creates files, pipelines, mail, communication services, equipment, and console declaration form: Declare Function CreateFile Lib "kernel32" Alias ​​"CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Description: this The return value of the function is long, if the execution is successful, return the file handle. Invalid_Handle_Value indicates an error and set GetLastError. Even if the function is successful, if the file exists and specifies CREATE_ALWAYS or OPEN_ALWAYS, GetLastError is also set to ERROR_ALREADY_EXISTS to open a communication port (such as COM1), whether it is set to Open_EXISTING. This function replaces the Lopen and LCREATE functions, it should be our preferred parameter description:

Parameter Type and Description LPFileNameString, the name DWDesiredAccessLong to be opened, if you want to read access to the device; if you want to write access to the generic_write (can be used); if it is zero, it means that only allows access to a device information dwShareModeLong related zero indicates that no sharing; FILE_SHARE_READ and / or FILE_SHARE_WRITE representation allows the file sharing access lpSecurityAttributesSECURITY_ATTRIBUTES, a pointer to a SECURITY_ATTRIBUTES structure, defines the security profile (if the operating system supports it) dwCreationDispositionLong, the following One of the constants: create_new creates a file; if there is a file, it will be error CREATE_ALWAYS to create a file, which will rewrite the previous file Open_EXISTING file must already exist. By applying Open_ALWAYS, Open_ALWAYS, if you don't exist, create it truncate_existing, show that existing files are shortened to zero length dwflagsandattributeslong, one or more of the following constant file_attribute_attribute_compressed to mark files as compressed, or marked as files in the directory the default compression FILE_ATTRIBUTE_NORMAL default properties FILE_ATTRIBUTE_HIDDEN hidden files or directories FILE_ATTRIBUTE_READONLY file is read-only file FILE_ATTRIBUTE_SYSTEM not postpone FILE_FLAG_OVERLAPPED write to a file is a system file FILE_FLAG_WRITE_THROUGH operating system allows the file to prohibit overlapping operations FILE_FLAG_NO_BUFFERING file buffered. The file can only be written to the disk volume. File_flag_random_access optimizes file_flag_sequential_scan for random access to file buffer File_Flag_sequence-in-Soft File_Flag_Delete_on_close to close the file buffer for continuous access to the last open handle, turn the file. Delete. Particularly suitable for temporary files may also be used in combination at the following constants labeled Windows NT: SECURITY_ANONYMOUS, SECURITY_IDENTIFICATION, SECURITY_IMPERSONATION, SECURITY_DELEGATION, SECURITY_CONTEXT_TRACKING, SECURITY_EFFECTIVE_ONLYhTemplateFileLong, if not zero, specifies a file handle. The new file will copy the extended attribute from this file as follows: long hfile = createfile ("c: //test.txt", generic_read, file_share_read | file_share_write, null, open_existing, file_elag_overlapped, null 2, LCREAT effect: Create a file . If the file already exists, it will shorten it to zero length and open it to read and write declaration:

Declare function lcreat lib "kernel32" alias "_lcreat" (Byval LPPathname As String, BYVAL IAATTRIBUTE AS LONG) AS LONG Description: This function returns the value of long, such as the execution, returns the handle of the file. If an error is wrong, returning a hfile_error This function opens the file that has been opened by other applications, so be careful when using it. Win32's CREATEFILE function has replaced this function. This function is the same as the VB's Open statement: Parameter Type and Description LPPathNameString, the name IATtributelong of the file to create, one of the following values: 0-- file can read and write 1 - Create a read-only file 2 - creation Hide File 3 - Creating a system file, such as the following statement opens a c: /test.txt file

LCREAT "C: / Test.txt", 0 3, Lopen Role: Open the specified file declaration form in binary mode:

Declare function lopen lib "kernel32" Alias ​​"_link Hfile_Error indicates an error. Setting the getLastError CreateFile function provides more features under Win32: Parameter Description:

Parameter Type and Description LPPathNameString, a combination of name IReadwritelong, access mode, and shared mode constant, as follows: 1. Access mode read open files, read and write WRITE Open the file, write content 2, shared mode (refer to the OpenFile function logging constant table) OF_SHARE_COMPAT, OF_SHARE_DENY_NONE_EXCLUSIVE 4, OF_SHARE_DENY_WRITE, OF_SHARE_EXCLUSIVE 4, GETFILETIME Role: Time information declaration form for specified files:

Declare Function GetFileTime Lib "kernel32" Alias ​​"GetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long Description: Long, non-zero for success and zero for failure. Setting getLastError If you don't need specific information, then LPCREATIONTIME, LPLASTACCESSTIME, LPLASTWRITETIME can be set to zero (with ByVal as long). The file time returned by this function uses a UTC format parameter description:

Parameter Type and Description Hfilelong, file handle LPCREATIONTimeFileTime, creation time LplastaccesstimeFiletime for loading files, time for loading files (FAT file system does not support this feature) LplastWRittimeFileTime, used to load files last modified time sample code is as follows: 'function declaration Private declare function GetFileTime Lib "kernel32" (ByVal hFile as Long, lpCreationTime as FileTime, lpLastAccessTime as FileTime, lpLastWriteTime as FileTime) as LongPrivate declare function lopen Lib "kernel32" Alias ​​"_lopen" (ByVal lpPathName as String, ByVal iReadWrite As Long) As Long 'define the variable Dim file As LongDim CreationTime As FileTimeDim lastaccesstime As FileTimeDim lastaccesstime As FileTime' definition structure Private Type FileTime dwLowDateTime As Long dwHighDateTime As LongEnd TypePrivate Sub Form_Load () str1 = "c: / text. TXT "file = limited (str1, read_write) 'Open file temp = getfiletime (file, credition, lastaccesstime)' Get file related information MSGBox CreationTime 'Display information END SUB or above code display file creation time 5, CopyFile effect: copy file. Similar to VB's filecopy commands:

Declare Function CopyFile Lib "kernel32" Alias ​​"CopyFilea" (Byval LpnewFileName As String, Byval BfailifeName As String, Byval BfailifeXists As long) AS Long Description: Long, non-zero means success, zero means failure. Set the getLastError parameter description:

Parameter Type and Description LPEXISTINGENAMESTRING, source file name LPNewFileNameString, target file name bFailifeXistslong, if set to true (non-zero), then once the target file already exists, the function call will fail. Otherwise the target file is rewritten sample code as follows:

'Function declaration Private Declare Function CopyFile Lib "kernel32" Alias ​​"CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As LongPrivate Sub Form_Load () CopyFile "c: /test1.txt", "c: / Test2.txt ", 1End Sub Copy C: /Test1.txt to C: / Test2.txt6 ,movefile, MoveFileEx effect: Move files. As dwFlags set to zero, MoveFile fully equivalent to MoveFileEx declaration form: Declare Function MoveFile & Lib "kernel32" Alias ​​"MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) Declare Function MoveFileEx & Lib "kernel32" Alias ​​"MoveFileExA" ( Byval lpexistingfilename as string, byval lpnewfilename as string, byval dwflags as ring) Description: long, non-zero means success, zero means failure. The two functions that will set the getLastError usually cannot move files from one volume to another. But if the movefile_copy_allowed tag is set, MoveFileEx can do this. Parameter description:

parameter

Type and description

lpExistingFileNameString, the file name to be moved lpNewFileNameString, new file name dwFlagsLong, one or more of the following constants MOVEFILE_REPLACE_EXISTING as the target file exists, it will replace MOVEFILE_COPY_ALLOWED mobile to a different volume, the file copy and delete the original file movement MOVEFILE_DELAY_UNTIL_REBOOT Operation is officially performed when the system is restarted next. This can be changed in Windows NT as follows:

'Definition structure Private Const MOVEFILE_COPY_ALLOWED = & H2Private Const MOVEFILE_DELAY_UNTIL_REBOOT = & H4Private Const MOVEFILE_REPLACE_EXISTING = & H1' function declaration Private Declare Function MoveFile & Lib "kernel32" Alias ​​"MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) Private Declare Function MoveFileEx & Lib "kernel32" alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) Private Sub Form_Load () MoveFile "c: /test.txt", "d: /test1.txt" 'moving file MoveFileEx "d: /TEST1.TXT "," c: /test.txt ", movefile_replace_exiSTING 'Return END SUB The above code implements the file movement, after moving two times, the file is unchanged 7, the DeleteFile effect: Delete the specified file declaration form: Declare Function deletefile lib "kernel32" alias "deletefilea" (Byval LPFileName As String) AS Long Description: long, non-zero means success, zero means failure. GetlasTERROR is similar to the VB's kill statement, using this function under Windows 95 - Even if the file is currently turned on by an application, the function will also delete it. Parameter description:

Parameter types and instructions LPFileNameString, the name code for deleting files is as follows:

'Function Declaration Private Declare Function Deletefile LIB "Keernel32" Alias ​​"Deletefilea" (Byval LPFileAns String) AS Longprivate Sub Form_Load () DeleteFile "c: /test.txt" End Sub above code delete C: /Test.txt file write There is not much, it is really tired, so I hope that my hard work has not been in a white fee. Although VB is a RAD class tool and makes many people who don't use it, but try to say that VB is really powerful, especially humanity. The design makes people love, I hope to give VB's enthusiasts can have a new understanding of VB, not only to use the existing controls provided.