I need to check if a file is in use before I try to get access to it. Is there way to do it in Lazarus environment?
Lazarus: How to check if file is in use?
3.1k Views Asked by ThN At
2
There are 2 best solutions below
1
Ken White
On
You don't. You try to open it, and handle the exception if and when you can't.
Checking to see if it's in use first serves no purpose.
Your code to see if it's in use says it's not
--->>> Another app opens the file, locking it
Your code to open file fails
Instead, use a normal try..except block:
try
FS := TFileStream.Create(YourFileName, fmOpenReadWrite, fmShareExclusive);
// Code to use file here
except
// Handle failure to access file
end;
Related Questions in PASCAL
- Exception raised when setting caption property in next form by user-defined procedure
- Not enough storage is available to complete this operation JclCompression
- How to get the parent directory path of a path in FreePascal/Lazarus?
- How to debug Pascal runtime error?
- find generic path of MySQL in registry as it creates version specific entry as key in registry
- How to write an array to a .dat with pascal
- Pascal error - ";" expected but Else found
- Pascal: change TProcess description?
- Testing the type of a generic in delphi
- How to generate a symbol table for a Pascal-subset compiler in C?
- Running Innosetup installer in Windows 10
- Math and physic in programing
- Invalid procedure or function reference - Pascal
- Comparing a char variable to empty char does not work
- Taking Valid Enumeration DataType Input via Loop
Related Questions in LAZARUS
- Exception raised when setting caption property in next form by user-defined procedure
- How to get the parent directory path of a path in FreePascal/Lazarus?
- How to get running process in Lazarus
- Threads: Send data to a specific active thread
- Lazarus/FreePascal, Synapse send file to TCPBlockSocket
- Making an SDL Viewport with LCL
- Odd sharing access violation (OS Error 32) with Freepascal implementation of libEWF
- Custom TEdit with input restrictions
- Math and physic in programing
- Lazarus: fatal error when opening an "output" procedure in another procedure
- How to make link button in Lazarus?
- Remove delay when continuously pressing a key
- Not Run wince Lazarus TI Cortex-A9
- glReadPixels always returns a black image
- True free heap not what it should be after ShellExecute
Related Questions in FILE-ACCESS
- Using access() in C
- Access Denied when trying to open with File.OpenRead
- How to Write to a Certain Line in a File in VB
- Virtuoso ISQL data import can't stat file
- working from several threads on the same file
- UnauthorizedAccessException on android
- How to increase the speed of a php function that returns a token
- Is there any way to know that FileSystemInfo.Refresh failed?
- System.IO.IOException error, can't get access to the file
- JSON Directory index with PHP - file access?
- System.UnauthorizedAccessException was unhandled
- How to access the client system in asp.net mvc
- Windows Phone 8 UnauthorizedAccessException when opening file
- Encrypting .txt File Erroring - Process Cannot Access File
- File only accessible when placed in a location where it will be erased from the jar
Related Questions in FILE-IN-USE
- cannot save file because it's in use
- Get permission denied error when trying to remove file
- Using a MemoryStream to Load .jpg without blocking the file by process?
- File in use by another process error when uploading a file using server but not localhost
- Android detect other process writing to file
- Resolving 'File in Use' Error During IIS Deployment in Azure DevOps
- The process cannot access the file because it is being used by another process at deletion
- File is Still in Use Error 32 How can I free it?
- Scheduled BAT - Error during XCOPY if file is in use
- Program error-file in use in visual foxpro
- How to close the file after sending emails with an attachment?
- Remove-Item (and [System.IO.File]::Delete() ) removes file that is in use
- Upgrading from a wildcard app ID to an explicit app ID to allow push notifications
- Automatically copy files in use
- Resolving exception file in use after closing StreamWriter
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Open the file with
FileOpen(FileName, fmOpenReadWrite or fmShareExclusive)and check the result.Update (thanks to Ken White's comment). This is not the direct answer to your question as
FileOpenactually gets access to the file, but you shouldn't perform the check and then open the file, otherwise you'll get a race condition. You should open the file and then check if the open succeeded.Opening a file with
FileOpenand accessing the file through its handle may seem unfamiliar. You can achieve the same goal when opening a file with e.g.Rewriteinside atry-exceptblock. I am not sure about Lazarus, but in Delphi usingtry-exceptwith high-level file routines requires explicit resetting IOResult in case of exception (putSetInOutRes(0)intoexceptsection), otherwise the following file operation fails.