Is there any way to know if a particular file is open/in use/locked by another application/process?



  • I want to know if a file is in use in another application. I tried to look around QFile, QProcess but there seems no way to find out. I am trying to do some text file manipulation which is used constantly by many applications. Before doing anything I would like to know if it's in use/open/locked by other app.


  • Moderators

    Do you want to lock the file before using it?
    In this case you can use http://doc.qt.io/qt-5/qlockfile.html


  • Qt Champions 2016

    To add to @jsulm's excellent suggestion, you should tell what OS you're intending on using this on, and what are these "many applications". Are these applications written by you, are they using Qt?

    Kind regards.



  • As long as the other (external) application / process locks the file you want to access, the solution @jsulm suggested will work.


  • Qt Champions 2016

    @the_

    will work

    Such a strong word ... it depends on the advisory locking mechanism used and most linux-es will support more than one. As duly noted in the documentation it's only guaranteed to work if the same scheme is used across all processes:

    Serialization is only guaranteed if all processes that access the shared resource use QLockFile, with the same file path.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.