Procedure tform1.button1click (Sender: tobject );
Begin
If opendialog1.execute then
Begin
If isfileinuse (opendialog1.filename) = true then
Showmessage ('file in use ')
Else
Showmessage ('file not use ');
End;
End;
Function tform1.isfileinuse (fname: string): Boolean;
VaR
Hfileres: hfile;
Begin
Result: = false; // The returned value is false (that is, the object is not 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 fails to return, the result is true (that is, the file is in use)
If not result then // If the createfile function returns success
Closehandle (hfileres); // close the handle.
End;
End.
Bytes ----------------------------------------------------------------------------------------------------------------------
First, let's first understand the createfile function. Its prototype is as follows: handle createfile (
Lpctstr
Lpfilename, // Pointer to the file name
DWORD
Dwdesiredaccess, // Access mode (write/read)
DWORD
Dww.mode, // Share mode
Lpsecurity_attributes
Lpsecurityattributes, // Pointer to the Security Attribute
DWORD
Dwcreationdisposition, // How to create
DWORD
Dwflagsandattributes, // File attributes
Handle
Htemplatefile// Used to copy the file handle
); The following describes the parameters.
Lpfilename
Point to an empty ending string. This parameter specifies the object used to create or open the handle.
If the lpfilename object is a path, there is a limit on the maximum number of characters. It cannot exceed the constant (max_path). This limit indicates how the createfile function parses the path.
Dwdesiredaccess
The access method of the specified object. The program can obtain read, write, read/write access, or device query access.
This parameter can be any combination of the following values 0
The program can query the properties of a device without directly accessing the device. generic_read
Read access. You can read data from a file and move the file pointer. You can combine it with generic_write to become "read/write access". generic_write
Write access. You can write data from a file and move the file pointer. You can combine it with generic_read to become read/write access ".
Dww.modeThe Set bit flag indicates how the object is shared. if the parameter is 0, the object cannot be shared. subsequent operations to open an object 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_pai_delete
Windows NT: subsequent open operations that only request to delete access permissions will succeed. file_assist_read
Subsequent open operations that only request read access will succeed. file_assist_write
Subsequent open operations that only request write access will succeed.
Lpsecurityattributes
A pointer to a security_attributes structure is used to determine how to inherit the handle from a child process. If this parameter is null, the handle cannot be inherited.
Dwcreationdisposition
Specifies the action when the file exists or does not exist. For more information about this parameter, see the annotations section. This parameter must be one or more of the following values. Create_new
Create a new file. If the file already has a function, it will fail.
Create_always
Create a new file. If the file already exists, the function will overwrite the existing file and clear the existing file attribute open_existing.
Open a file. If the file does not exist, the function will fail.
If you use the createfile function to load the console for the device, check the section "Why use the open_existing flag" in the annotation.
Open_always
If the file exists, open the file. If the file does not exist and the create_new flag exists in the parameter, create the file. truncate_existing
Open a file. Each time the file is opened, it will be 0 bytes. The Calling process must be in generic_write access mode.
Open file. If the file does not exist, the function will fail.
Dwflagsandatributes
Specify the attribute and flag for the file. This parameter can receive any combination of the following attributes. Unless all other file attributes ignore file_attribute_normal. file_attribute_archive
The file will be archived. The program uses this attribute to indicate that the file is backed up or removed from file_attribute_hidden.
The file is hidden. It is not loaded in the general folder list. file_attribute_normal
The file is not set with any properties. file_attribute_offline
File data is not used immediately. Indicates that the file is being used offline. File_attribute_readonly
This file can only be read. The program can read the file, but cannot write content on it or delete. file_attribute_system
The file is part of the system or is dedicated to. file_attribute_temporary.
After a file is used, the file system will try to maintain a block for fast access to all data (file ).
Memory. Temporary files should be deleted in time when the program is unavailable.
DwflagandattributesAny combination of the following logos is acceptable. File_flag_write_through
Indicates that the system writes data directly to the disk through the cache. file_flag_overlapped
Indicates the system initialization object. This operation sets a reference count for the process and returns error_io_pending. after processing, the specified object is set to the signal state. when you specify file_flag_overlapped, the function for reading and writing files must specify an overlapped structure. and. when file_flag_overlapped is specified, the program must execute overlapping parameters (pointing to the overlapped structure) to read and write files. this flag can also be executed by more than one operation. file_flag_no_buffering
Indicates that the system does not use a fast buffer or cache. When combined with file_flag_overlapped, this flag provides the maximum Asynchronous Operation volume because I/O does not rely 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, the program must meet the following requirements:
- The Byte offset starting with file access must be an integer multiple of the slice size.
- The number of bytes for file access must be an integral multiple of the slice size. for example, if the slice size is 512 bytes, the program can read or write 2048, 335,981, or 7171 bytes, but cannot be or bytes.
- The address used for read and write operations must be aligned with the slice. In the memory, the alignment address is an integral multiple of the slice size.
- One way to align the buffer with the slice size is to use the virtualalloc function. it allocates the memory address that is aligned with the integer of the operating system memory page size. because the memory page size and fan area size-2 are their power.
- The inner address is also aligned with the integral multiple of the slice size.
- The program can call getdiskfreespace to determine the size of the slice.
File_flag_random_access
The specified file is randomly accessed. This flag can buffer the system optimized file. file_flag_sequential_scan
The specified file is continuously accessed from start to end. this flag prompts the system to optimize the File Buffer. optimization may not occur if the program moves the file pointer during random access to the file; however, correct operations can still be ensured.
Specifying this flag can improve the performance of the program to read large files in sequential access mode. The performance improvement is obvious when many programs read large ordered files. however, there may be a small number of bytes missing. file_flag_delete_on_close
Indicates that the system will immediately delete the file after all open handles of the file are closed. Not only can you specify file_flag_delete_on_close. If file_assist_delete is not used, subsequent requests to open the file will fail. file_flag_backup_semantics
Windows NT: indicates that the system opens or creates a file to perform a backup or recovery operation. the system ensures that the calling process ignores the security options of files, if it must have a privilege. the related privileges are se_backup_name and se_restore_name. you can also use this flag to obtain a folder handle. A folder handle can be passed to some Win32 functions like a file handle. File_flag_posix_semantics
Specifies that the file complies with POSIX standards. This is the standard in MS-DOS and 16-bit windows. file_flag_open_reparse_point
Specify this flag to restrict the NTFS partition pointer. This flag cannot be used with creat_always. file_flag_open_no_recall
Specifies the file data to be received from the remote storage, but it will not store the data in the local storage.
This flag is used by the remote storage system or Hierarchical Storage Manager System.
Htemplatefile
Specify a handle to the template file for the generic_read access mode. The template file provides the file attributes and extended attributes after the file is created.
Return values
Return value if the function is successful, return 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, call getlasterror to return error_already_exists (indicating that the function is successful ). if the function file does not exist before the call, 0 is returned. if the function fails, the return value will be invalid_handle_value. for more error information, call getlasterror.