0

I've some troubles with a program (straus7) that don't check if a file is already open. This is a problem because often the files are stored in the server and they can be opened by multiple users.

I was wondering if it is possible to catch when a specific extension of a file (in my case .st7) is opening, then check if it is stored in the server and finally check if a temporary file with the same name with the temp extension already exists. If already exists, I just want to show a message box "The file is already open".

I'm not at all a developer, I've written some codes in C#, maybe is that not the right way to solve the problem? I've maybe to do an application that handle the opening of the files and then do this procedure easyer?

Thanks for any help

Giovanni
  • 13
  • 5
  • `File.Exists(path)` to check if a file exists if it exists it will return `true` else it will return `false`. Does the directory path ever change? Or do you want to do a system wide search? –  Nov 28 '18 at 17:13
  • 1
    @Matt what if right after that call returns false someone opens the file? – Steve Nov 28 '18 at 17:16
  • @Steve Then he will fail :) – JohnyL Nov 28 '18 at 17:17
  • @Steve Race conditions, who wins wins, not sure if you can lock the file, or so a Process.Start() to open the file directly after checking if it can be opened –  Nov 28 '18 at 17:17
  • 2
    Just to be clear, that's an unavoidable race condition. You need to code for the chance that might happen. – Flydog57 Nov 28 '18 at 17:21
  • @Flydog57 no its not. Its totally avoidable if you acquire an exclusive lock on the file. – Steve Nov 28 '18 at 17:22
  • 2
    I would suggest OP handing this task to a real dev. Since this involves concurrency and its a complex topic to cover. Implementation depends on your specific need and use case. – Steve Nov 28 '18 at 17:26
  • Possible duplicate: https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use, which has a mix of good and bad advice. For example, the accepted answer is wrong, as it does not take into account race conditions. Spence gives a better answer, but does not give much explanation. – Polyfun Nov 28 '18 at 17:37
  • Thank you guys for the answers. The real problem I think is that I would understand when a type of file (.st) is opening, because I don't know when lunch my "checking application" if I don't handle the opening of the file. I thought it was possible to use something like an "event handler" monitoring the system process but I found it isn't. – Giovanni Nov 29 '18 at 08:35
  • @Steve yes, probably I'm not in a capacity to solve this problem. – Giovanni Nov 29 '18 at 08:35
  • @Polyfun I can't use a 'try..catch' statement, because the file will still open. – Giovanni Nov 29 '18 at 08:35

0 Answers0