-2

I am developing an application (using C#) that should check if a file is locked by another process or not. At the current stage of the implementation, I could detect if a PDF file is locked by Adobe reader. However, it's not possible when it's locked by a browser (e.g Mozilla FireFox).

I have tried the proposed solutions in the below stackoverflow threads, but all of them failed:

Do you have any other suggestions ?

dymanoid
  • 14,771
  • 4
  • 36
  • 64
K.chaa
  • 65
  • 11
  • There's no guarantee that any particular tool will keep a file handle open/maintain a lock after having loaded it. That's entirely an implementation detail that each application can make different decisions about. There's nothing mandated at the OS level. – Damien_The_Unbeliever Oct 29 '18 at 15:46
  • Without more details on why the existing solutions "fail" and "aren't possible", it's going to be hard for anyone to suggest alternatives. First and foremost, have you considered the possibility that the file isn't locked at all? A quick test on my machine suggest Firefox reads the entire file up front and keeps no open handle to it at all, let alone one that locks. Indeed, I can delete the file and still continue to read it. – Jeroen Mostert Oct 29 '18 at 15:47
  • But when trying to delete a file that is locked by a navigator, you cannot perform this operation. That means there is something locked by the process but I cannot detect it through c# code. – K.chaa Oct 29 '18 at 15:49
  • On my machine, Firefox maintains no open handles to a PDF file after having opened it, and so I *can* delete the file. Either your example is poorly chosen, or on your machine there's something else keeping the file open. You can use Process Explorer to find open handles; try it before you conclude that code isn't working correctly. – Jeroen Mostert Oct 29 '18 at 15:54
  • I think that I didn't explain well the problem. I am building a setup using installshield that should check if any file of the generated files during the installation are locked or not before the uninstall because if a file is locked by other process the setup will continue but the file will not be deleted. I implemented a c# class library that will be called from the installshield script and that should check if a file is locked or not. When a pdf file is locked by adobe reader i can detect this and inform the user that he should close this file, but when it is open by a navigator the setup – K.chaa Oct 29 '18 at 16:04
  • but when it is open by a navigator the setup could not detect the this file is locked and continue the uninstall. – K.chaa Oct 29 '18 at 16:05
  • Does the uninstall succeed in this case (as in, delete the file) or fail? If it succeeds, that's proof that the file was not locked in the first place. You do not need to inform the user to close the file because they don't actually need to! If it fails, you've got an actual issue, but then you should verify why the Installshield setup isn't deleting the file. Does it log an error code somewhere? Can *you* delete the file manually after Installshield fails to? – Jeroen Mostert Oct 29 '18 at 16:09
  • 2
    InstallShield has inbuilt support for locked files - please see http://helpnet.flexerasoftware.com/installshield19helplib/Subsystems/installshield19langref/helplibrary/LangrefOnFileLocked.htm. Return ERR_PERFORM_AFTER_REBOOT from the OnFileLocked event to delete the file after a reboot. – Polyfun Oct 29 '18 at 16:24
  • @Polyfun can you put your comment as an answer to change this thread status to resolved? – K.chaa Oct 30 '18 at 14:07

1 Answers1

0

InstallShield has inbuilt support for locked files - please see http://helpnet.flexerasoftware.com/installshield19helplib/Subsystems/installshield19langref/helplibrary/LangrefOnFileLocked.htm. Return ERR_PERFORM_AFTER_REBOOT from the OnFileLocked event to delete the file after a reboot.

Polyfun
  • 9,479
  • 4
  • 31
  • 39