3

I want to know if a file was opened by user for reading (double click or open with ...), I am coding a C++ application with Qt Creator on Windows, after some research I found QFileSystemWatche, but it let me know only if a change was happened in the specific folder.

void QFileSystemWatcher::fileChanged ( const QString & path ) [signal] This signal is emitted when the file at the specified path is modified, renamed or removed from disk.

How to know if the file was opened? or is there a way to modify a file when it is opened or closed?

Any idea please!!!

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Oumaya
  • 655
  • 4
  • 18
  • 43
  • It seems that you can't see here why: http://lists.trolltech.com/qt-interest/2006-01/msg00251.html – tune2fs Oct 11 '12 at 14:30
  • @tune2fs my purpose is to know if a file was opened the moment when the user click on or open it in a program , some how connect the signal of opening it to a slot , I hope this is clearer. – Oumaya Oct 11 '12 at 14:35
  • The question "has a file been opened" is not well-defined; if, for example, the file resides on a shared drive (NAS storage), and another remote computer accesses it, you couldn't get a notification because no networked filesystem has server-pushed callbacks for this purpose. Also, if someone opens the file and immediately closes it again, would it be correct to notify you ? Generically ... this is information you can extract via system tracing/debugging utilities (SysInternals procmon / filemon) only - for a reason. – FrankH. Oct 11 '12 at 14:41
  • am working for created local files and I need to handle a notification when the user open it that's my point. – Oumaya Oct 11 '12 at 14:51

1 Answers1

0

2 solutions:

  • Using Qt

You can make the following function (pseudocode):

function is_open(file)
    handle = open file to write
    if(handle is ok)
        close file
        return true
    else
        return false

Then you call it once per second, and emit a signal fileOpened(const QString& file) whenever is_open(t-1) == false && is_open(t) == true, where t is the time.

However, this solution might be slow, and can result is some io-access bloat, especially if the file is on a distant server. It can also cause a premature wear of the disk/SSD, so I'm not recommending this solution.

  • Using system API

You can get get the list of all file handles of all processus, then checking if it contains a handle to the file you want to monitor. Again, you will have to do this x times per second. It will slow your pc; it is not really portable in practice (the code needed heavily depends on system-specific API), and it might need elevated priviledge to work. I don't recommend that too...

Synxis
  • 9,236
  • 2
  • 42
  • 64
  • thanks @Synxis for your solution, but I think it's so hard to accept it, as you said it can slow my pc and I want an optimal solution that let me know if a user click on a file to open it or a process try to open it and read data from the disk, thank you any way :) – Oumaya Oct 12 '12 at 07:38
  • By the way, the only fast solution I know is to write a file-system driver, which is way more work than the feature you want to have. – Synxis Oct 12 '12 at 11:16
  • yeah you are totally right, I had the idea of creating a file-system driver or a filter system driver but I am newbie, I'm pressed by time and I know nothing about WDK, so I asked for the easy way to do that. – Oumaya Oct 12 '12 at 11:53