C# 有没有办法知道用户何时完成使用应用程序编辑文件?

发布于 2024-10-10 14:22:00 字数 438 浏览 2 评论 0原文

这就是我想要做的:

  1. 从服务器下载文件(txt,doc,xls,等等)
  2. 使用 System.Diagnostics.Process.Start(文件路径)使用适当的应用程序打开文件
  3. 使用监视文件更改文件系统观察者。
  4. 每次更改文件时,将文件上传回服务器
  5. 继续监视,直到用户完成文件编辑
  6. 删除文件的本地副本
  7. 退出应用程序

我卡在第 5 步。如何知道用户是否已更改完成文件处理了吗?

我不能依赖被锁定的文件(例如记事本不锁定 txt 文件)。

我不能依赖进程是否退出(一个例子是用于 txt 文件的 Notepad++:该文件可以在选项卡中打开。当您关闭选项卡时,您已经完成了文件的编辑,但该进程仍在运行)

任何关于如何在 C# 中做到这一点的想法/要点?

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 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# ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走野 2024-10-17 14:22:00

您已经排除了两种可以检测正在使用的文件的方法:文件锁定和您开始退出的进程。

我想到的唯一替代方案是显示一个对话框,询问用户何时完成编辑。

编辑:就其价值而言 - FileZilla 有这种类型的行为。您可以选择编辑远程服务器上的文件,它会下载该文件,启动默认编辑器,并(在后台)显示“如果您已完成编辑 - 单击确定”按钮。
如果我弄乱了文件并保存了它,这使我有机会取消编辑。

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.

卸妝后依然美 2024-10-17 14:22:00

这确实很难做到——我们尝试了各种方法,但从未发现任何万无一失的方法。如果您知道您启动的程序,那么理论上您可以找到它使用的文件句柄,并查看它何时停止使用您感兴趣的程序......但是如果您依赖 Windows 来解析默认应用程序即使启动这个也变得很棘手。

我们将可编辑文件复制到以日期命名的临时文件夹中,并依赖用户在完成编辑会话后将它们上传回来。然后,我们在应用程序启动时清理前几天的文件夹。

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.

弱骨蛰伏 2024-10-17 14:22:00

您可以检查文件的上次更改日期。当您保存对文件的更改时,会设置此日期。请注意,该字段不是很可靠,因为可以将其设置为任何值(使用适当的工具)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文