The LockFile API takes a file handle. I normally use TStream for file access, so I’m unsure how to get the appropriate handle, given an ANSIString filename only. My purpose is to lock a file (which may not exist originally) during a process, write some information to other users, and then unlock and delete it.
I would appreciate sample code or pointers to it to make this reliable.
You can use the LockFile function in conjunction with CreateFile and UnlockFile functions.
See this example
Another option , if you want to lock the file using TFileStream you can opening the file using exclusive access (fmShareExclusive).
Note : in both examples the access is read-only, you must change the flags in order to write the files.