Delphi to determine if the file is being used

Source: Internet
Author: User

First, let's begin by understanding the CreateFile function, which is prototyped as follows HANDLE CreateFile (
Lpctstr lpFileName,//Pointer to file name
DWORD dwdesiredaccess,//access mode (write/Read)
DWORD dwShareMode,//Sharing mode
Lpsecurity_attributes lpsecurityattributes,//Pointer to a security attribute
DWORD dwcreationdisposition,//How to create
DWORD dwFlagsAndAttributes,//File properties
HANDLE hTemplateFileUsed to copy a file handle
); The following is a description of the parameter lpFileName
Points to a null-terminated string. This parameter specifies the object used to create or open a handle.
If the lpFileName object is a path, there is a limit of the maximum number of characters. Cannot exceed constant (MAX_PATH). This limit indicates how the CreateFile function parses the path. dwdesiredaccess
Specifies how the object is accessed, and the program can gain read access, write access, read-write access, or query device ("Device query") access.
This parameter can be any combination of the following values 0
Specifies the request access. The program can query the properties of the device without directly accessing the device. Generic_read
Specifies read access. You can read the data from the file and move the file pointer. Can be combined with generic_write to "read and write access". Generic_write
Specifies write access. You can write data from a file and move the file pointer. Can be combined with generic_read as read-write access. dwShareModeSetting a bit flag indicates how the object is shared. If the parameter is 0, the object cannot be shared.     Subsequent open object operations will fail until the object's handle is closed.     Use a combination of one or more of the following values to share an object. File_share_delete
WINDOWS NT: Subsequent open operations that simply request the removal of access will succeed. File_share_read
Subsequent open operations that simply request read access will succeed. File_share_write
Subsequent open operations that simply request write access will succeed. lpsecurityattributes
A pointer to a security_attributes structure is used to determine how to inherit the handle in a child process. If this parameter is NULL, the handle is not inheritable.
dwcreationdisposition
Specifies how to act when a file exists or does not exist. For more information on this parameter, refer to the comment section.     This parameter must be one or more of the following values. Create_new
Create a new file. If the file already exists, the function fails.

Create_always
Creates a new file. If the file already exists, the function overwrites the existing file and clears the existing file attribute open_existing
Open a file and the function will fail if the file does not exist.
If you use the CreateFile function to load the console for your device. See the section "Why use the Open_existing flag" in comments.

Open_always
If the file exists, open the file.     If the file does not exist and the parameter has the CREATE_NEW flag, the file is created. Truncate_existing
Open a file, each time it opens, the file will be up to 0 bytes. The calling process must be hit with generic_write access mode
Open the file. If the file does not exist, the function fails. dwflagsandatributes
Specify attributes and flag bits for a file This parameter can receive any combination of the following properties. Unless all other file attributes are ignored File_attribute_normal. File_attribute_archive
The file is archived and the program uses this property to flag the file for backup or removal File_attribute_hidden
The file is hidden and will not be loaded in the General Folder List. File_attribute_normal
The file is not set any properties.
File_attribute_offline
The data for the file is not immediately available. Indicates that the file is being used offline. File_attribute_readonly
This file is readable only. The program can read the file, but it cannot be written on it, nor can it be deleted. File_attribute_system
Files are part of a system, or system-specific. File_attribute_temporary
After the file is used, the file system will strive to maintain a piece of all data for the (file's) WebEx access
Memory. Temporary files should be deleted in a timely manner when the program is not used. dwflagandattributesYou can accept any combination of the following flags.
File_flag_write_through
Instructs the system to write directly to the disk through a fast cache. File_flag_overlapped
Indicates the system initialization object, which sets a reference count on the process and returns error_io_pending. When processing is complete, the specified object is set to the signal State. When you specify File_flag_overlapped,    A function that reads and writes a file must specify a overlapped structure. and. When file_flag_overlapped is specified, the program must perform overlapping parameters (pointing to the overlapped structure) to read and write the file. This flag can also have more than one operation to perform. File_flag_no_buffering
Indicates that the system does not use fast buffers or caches, and when combined with file_flag_overlapped, this flag gives the maximum number of asynchronous operations, because I/O does not depend on the asynchronous operation of the memory manager. However, some I/O operations will run longer, Because the data is not controlled in the cache. When you use File_flag_no_buffering to open a file for work, the program must meet the following requirements:
    1. The byte offset at the beginning of the file's access must be an integer multiple of the sector size.
    2. The number of bytes of file access must be an integer multiple of the sector size. For example, if the sector size is a 512-byte program you can read or write 512,1024 or 2048 bytes, but not 335,981 or 7171 bytes.
    3. The address of the read and write operation must be in the aligned position of the sector, and the address aligned in memory is the integer multiple of the sector size.
    4. One way to align buffers with sector dimensions is to use the VirtualAlloc function. It allocates a memory address that is aligned to the full multiples of the operating system memory page size. Because the memory page size and sector size--2 are their powers.
    5. The existing address in this block is also aligned with the integer multiples of the sector size.
    6. The program can determine the size of the sector by calling GetDiskFreeSpace.
File_flag_random_access
The specified file is randomly accessed, and this flag allows the system to optimize the buffering of the files.
File_flag_sequential_scan
Specifies that the file will be accessed continuously from beginning to end. This flag can prompt the system to optimize file buffering. If the program moves the file pointer in a random-access file, the optimization may not occur; However, the correct operation can still be guaranteed
Specifying this flag can improve the program's ability to read large files in sequential access mode, and performance improvements are noticeable when many programs read large sequential files. But there may be a small range of bytes missing.
File_flag_delete_on_close
Instructs the system to delete files immediately after all open handles to the file are closed. No, only you can specify File_flag_delete_on_close if you do not use File_share_delete, subsequent requests to open the file will fail. File_flag_backup_semantics
WINDOWS NT: Instructs the system to perform a backup or restore operation for file opening or creation.       The system guarantees that the calling process ignores the security options for the file, if it must have a privilege. The associated privileges are se_backup_name and se_restore_name.     You can also use this flag to get a handle to a folder, and a folder handle can be passed to certain Win32 functions like a file handle. File_flag_posix_semantics
Indicates that the file is POSIX compliant. This is the standard under MS-DOS with 16-bit Windows. File_flag_open_reparse_point
Specifies that this flag restricts NTFS partition pointers. This flag cannot be used with creat_always. File_flag_open_no_recall
Indicates that file data is required, but will continue to be received from remote Storage. It does not store the data in local storage.
This flag is used by a remote Storage system or a hierarchical storage Manager system. hTemplateFile
Specifies a handle to the template file for Generic_read access mode. The template file provides file attributes and extended properties after the file has been created. Return Values
return value if the function succeeds, returns a handle to the specified file that is opened. If the specified file already exists before the function call and the dwcreation parameter is create_always or Open_always, the call GetLastError will return ERROR_ Already_exists (indicates a successful function).    If the function file does not exist before the call, it returns 0. If the function fails, the return value will be invalid_handle_value.  More error messages can be obtained by calling GetLastError. All right, there's a lot of whining about it. The function of "Judging whether a file is being used" is easier to understand, and functions as follows: function Isfileinuse (fname:string): boolean;
Var
Hfileres:hfile;
Begin
Result: = false; The return value is False (that is, the file is not being used)
If not fileexists (fName) then exit; Exit if the file does not exist
Hfileres: = CreateFile (Pchar (fName), generic_read or Generic_write,
0 {This is the trick!}, nil, open_existing, file_attribute_normal, 0);
Result: = (hfileres = INVALID_HANDLE_VALUE); If CreateFile returns failure then result is true (that is, the file is being used)
If not resultthen//If the CreateFile function returns to be successful
CloseHandle (Hfileres); Then close the handle
End Methods for calling Functions: isfileinuse (the file path to be judged);If the function returns true then it is in use, false then the file is not currently used reference: http://blog.csdn.net/sforiz/article/details/6918719

Delphi to determine if the file is being used

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.