Detect if file already opened in first instance of same application

Started by
5 comments, last by JerryM 15 years, 9 months ago
Helo everybody THis is C# problem. Scenerio: any file say test.txt is opened in my C# application fileopener.exe Now when i open another instance of fileopener and try to load test.txt. It should detect that test.txt is already opened. How can i do this. Please dont post the method of File.Open(.....) because this method was already tried. I want to know by using Process. i.e System.Diagonastics.Process or Mutex or combined
Advertisement
What about creating a temporary lock file in the same directory that has its attribute set to 'hidden' and its existence would mean that the file is currently open. When the file gets closed, that lock file gets deleted. Got the idea from process locks.
Quote:Original post by tariqwalji
What about creating a temporary lock file in the same directory that has its attribute set to 'hidden' and its existence would mean that the file is currently open. When the file gets closed, that lock file gets deleted. Got the idea from process locks.


Thanx I will try...it but other suggestions are also waited
Maybe implement a static list of open files in your fileopener class and use mutex lock on it. I am not very familiar with multithreading and if it would work properly.

*EDIT*
Sorry i just saw, that you are talking about executables rather than classes, so forget the above mentioned.

h.
You'd have to use shared memory, or named mutexes to do this sort of thing. You can't query the OS to find out if another specific process has opened a specific file (Not without a lot of hacking around, anyway), so you need to record this information yourself. The usual way of checking if another instance of your application is open is with a named mutex, but if you want to share other information (Such as a list of files), you'll need global shared memory (Or you can use WM_COPYDATA, or the C# equivalent).
Download Handle from sysinternals, and run "handle processname". :)

(And yes, there are horrible race condition issues with this).
Quote:Please dont post the method of File.Open(.....) because this method was already tried.
What was wrong with it? It works fine for me...

This topic is closed to new replies.

Advertisement