开发者

Is there a way in c# to know when the user has finished editing a file with an application?

This is what I'm trying to do :

  1. Download a file (txt, doc, xls, whatever) from a server
  2. Open the file with the appropriate application using System.Diagnostics.Process.Start(path to file)
  3. Monitor for file changes using a FileSystemWatcher.
  4. Each time the file is changed, upload the file back to the server
  5. Continue monitoring until the user has finished editing the file
  6. Delete the local copy of the file
  7. 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开发者_如何学JAVA 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.


This is really hard to do - we've tried various things but never found anything that was foolproof. If you know the program you have launched then, in theory, you can find the file handles it uses and see when it stops using the one you're interested in.....but if you rely on Windows to resolve the default application to launch even this becomes tricky.

We copy editable files into a temp folder named with the date and rely on users uploading them back when they have finished their edit session. We then clean up previous days folders on application startup.


You could check the date of last change of the file. This date gets set when you save changes to the file. Mind though that this field is not very reliable since one can set it to any value (with appropriate tools).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜