This is what I’m trying to do :
- Download a file (txt, doc, xls, whatever) from a server
- Open the file with the appropriate application using System.Diagnostics.Process.Start(path to file)
- Monitor for file changes using a FileSystemWatcher.
- Each time the file is changed, upload the file back to the server
- Continue monitoring until the user has finished editing the file
- Delete the local copy of the file
- Exit the application
I’m stuck at step 5. How can I know whether a user has finished working on a file ?
I cannot rely on the file being locked (notepad doesn’t lock txt files for example).
I cannot rely on a process having exited or not (an example is Notepad++ for txt files : the file could be open in a tab. When you close the tab, you’ve finished editing the file, but the process is still running)
Any idea/points on how to do that in C# ?
You’ve excluded the two ways you could go about detecting the file being in use: file locking, and the process you start exiting.
The only alternative I an think of is to display a dialog to ask the user when they’ve finished editing.
Edit: For what it’s worth – FileZilla has this type of behaviour. You can choose to edit a file on the remote server, it downloads the file, launches the default editor, and (in the background) shows a “If you’ve finished editing – Click OK” button.
This gives me the opportunity to cancel an edit, if I’ve mucked up the file and saved it.