ArielB
ArielB

Reputation: 1284

Identify when file opened using non-locking app is being opened/used by any application?

i'm trying to find the most generic way to understand if a file is being "used" by any application.

this is true also for applications like notepad/onenote/notepad++ (which don't lock the file).

i'm handling the opening of the files through my app (using Process.Start), and i need to delete the file when the user finishes working on it.

what i managed to come up until now is the following:

i'm totally lost about "Notepad++" applications or similar to them. notepad++ doesn't have any lock on the file or folder, and it uses single process. i 'm not sure how to handle this scenario, when doing Process.Start i don't even have a name of the process. (i can check the registry maybe to see who's the default app that opens the file)...

so, any other directions?

thanks!

== EDIT ==

well, currently i've decided about the following logic, unless there's a flaw in it:

  1. When opening an appliction (Process.Start) - save the PID and the application path. then wait 1 second (or something else i'll think about) - try to get the process by PID - if it exists, this is indeed the PID i want to wait on. if not, then i'll go by process path/name
  2. check if the file is locked by regular lock (by any application) - if so, then don't delete the file yet.
  3. if the file is not locked, check who's locking the folder - then compare it to the PID i have. if the PID doesn't exist (and it's indeed the PID i want to wait on) - then delete the file. if it exists - don't delete the file.
  4. If when starting the process after 1 second the PID doesn't exist, and no lock on the folder/file - i'll just wait until the process ends

that's the idea i think

Upvotes: 1

Views: 320

Answers (1)

Vikhram
Vikhram

Reputation: 4404

I am guessing you are trying to do this,

static void ShowFile(string fileToShow) {
    using (Process proc = new Process()) {
        ProcessStartInfo psi = new ProcessStartInfo(fileToShow);
        psi.UseShellExecute = true;
        proc.StartInfo = psi;
        proc.Start();
        proc.WaitForExit();
    }
}

But, maybe you should do this

static void ShowFile(string fileToShow) {
    using (Process proc = new Process()) {
        ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files (x86)\Notepad++\notepad++.exe", "-multiInst " + fileToShow);
        psi.UseShellExecute = false;
        proc.StartInfo = psi;
        proc.Start();
        proc.WaitForExit();
    }
}

And then when your function returns, you can delete your temp file.

I understand that you might not know if the system already has notepad++ installed and its path, etc. In that case you can try to use the path for notepad.exe which comes with windows.

The thing is, there is no perfect solution to what you are looking. To put it in other words, no file viewer/editor is guaranteed to

  1. Use a specific locking mechanism
  2. Keep handle open to the file or directory containing it
  3. Not communicate using DDE, etc

You can find out the default application registered to open your file, but you don't know if it starts as a single instance or a multi-instance app or they communicate using DDE, etc

Upvotes: 0

Related Questions