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
- write a code that divides a number into several different intervals and performs special calculations for each interval
- Cascade recursion - max element in array
- I search a unit to solve the turbo pascal 255 string limit
- Loading MP3 files into ListView. When multiple items are select, show <Multi>, need ability to save to each file even with <multi> showing
- anonymous thread with parameter in lazarus
- Plotting a polar rose — how to use "n" and "d" to achieve different shapes?
- No debugger via text IDE (Go32v2)
- Is there a way to initialize all items in a 2D array in Pascal on one line (without looping)?
- Lazarus Android Module Wizard in Free Pascal
- In Delphi, is it a correct approach to Free the Sender within an Event?
- How to Implement a Checksum Algorithm Based on a Written Description
- Why is this code used to draw a raycast vector not displaying the vector correctly?
- How do I properly assign integers in an array?
- How to handle libnodave error message with Delphi?
- How to find if a valid JSON string
Related Questions in LAZARUS
- anonymous thread with parameter in lazarus
- How to connect to VoiceRSS API with my Lazarus/Delphi program?
- Securing Password Management in TSQLite3Connection with SQLCipher
- Can I make a TIdMappedPortTCP proxy specific clients to different destinations?
- Lazarus Android Module Wizard in Free Pascal
- Why is this code used to draw a raycast vector not displaying the vector correctly?
- How to find if a valid JSON string
- How to get the names of the MIDI devices to the comboBox?
- Lazarus + "rsync -a ... ..." copy is timing out. How to fix?
- "Error while linking" while compiling empty project using Lazarus (fpc) 32bit on debian-based Linux
- Repeating the function until I release the CTRL button
- Load a Python module and call its functions within Delphi code without ExecString
- Uploading files to Synology NAS with frpascal code using FileStation API
- Lazarus RichMemo SetRangeColor not working as I expect
- Violation Acess when I try to Acess the Attribut Norden
Related Questions in FILE-ACCESS
- Failed to open for writing:java to filenotfoundException faield:EACCES(permission denied)
- Unity - Standalone .EXE file accessing from local file manager
- Crontab running the python program and python log file is not accessible as cron runs with sudo
- Access is denied. Exception from HRESULT: 0x80070005 (E_ACCESSDENIED) in UWP app
- PHP file access on localhost
- how to access files which is outside the var folder in linux server in code igniter 3 project
- Workarounds to access network drive files from Salesforce page?
- Next.js permission denied to package.json file after docker compose up
- React Native Android TV Box - Save Files on disk
- Problem with my C program that calculate grades in each term from .csv
- How to do an lseek on an open file, and read N bytes?
- SOLVED - Issue while using `new File(".")` when executing a new jar programmatically
- Recover data without correctly installed OS
- Auto download feature in ReactJs by accessing local folder
- I try to read file content from a file on a remote network path. I get the Error (it is being used by another process) How can I overcome this?
Related Questions in FILE-IN-USE
- List open files on Windows?
- Java check if removable drive in use
- Resolving 'File in Use' Error During IIS Deployment in Azure DevOps
- Error: ERROR_FILE_IN_USE when Deploying to IIS Server from Azure Pipeline
- IIS deployment error file is locked by an external process
- Get ProcessId that is using a file
- Automatically copy files in use
- Visual Studio C# How can I Load a Gridview Thumbnail without locking the source file for delete
- How to close the file after sending emails with an attachment?
- Program error-file in use in visual foxpro
- Scheduled BAT - Error during XCOPY if file is in use
- How to throw "The file is in use"
- Remove-Item (and [System.IO.File]::Delete() ) removes file that is in use
- How can I skip a workbook if it is in use and locked for editing?
- File remains in use until client refreshes page
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 # Hahtags
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.