使用后删除临时文件
我需要在 Windows Forms .NET 3.5 应用程序中使用一些临时文件。 这些文件在外部应用程序中打开,当然该应用程序的运行时间可能比我自己的程序更长。
是否有任何最佳实践来确保随时清理这些临时文件,以避免用户的硬盘充满不再需要的“垃圾”文件? 或者 Windows 是否会自动处理此问题?
一个很好的例子是任何邮件客户端:当您在任何应用程序中打开附件时,它通常会写入打开的临时文件中。 有没有办法弄清楚这些文件如何管理清理?
使用 Google 向我展示了很多很多清理和调整工具来手动清理临时目录,但我不想强迫用户这样做。 :-)
I need to work with some temporary files in my Windows Forms .NET 3.5 application. Those files are opened in an external application that can of course be running for a longer time than my own program.
Are there any best practices to make sure these temporary files are cleaned up at any time in order to avoid filling the user's hard disk with "junk" files which aren't needed anymore? Or does even Windows kind of handle this automatically?
A nice example is any mail client: When you open an attachment in any application, it is usually written to a temporary file which is opened. Is there a way to figure out how those files manage cleanup?
Using Google has shown me many, many cleanup and tune-up tools to clean the temp directory by hand, but I'd not like to force the users to do so. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 .NET 中,您可以使用
TempFileCollection
类,用于管理一组要由应用程序清理的临时文件(请注意,它相当隐藏在 CodeDom 命名空间中)。 显然,您无法删除不属于您自己的任何文件,因为这些文件可能仍被其他应用程序打开(删除它们可能会导致严重问题)。In .NET you can use the
TempFileCollection
class for managing a set of temporary files to be cleaned up by your application (Note that it is rather hidden in the CodeDom namespace). Obviously you can't delete any files not owned by yourself, because the files might still be opened by another application (and deleting them might cause serious issues).清理临时文件的责任应该由创建它们的应用程序承担。 这很容易。 我使用的类看起来像这样:
如果您需要清理另一个应用程序的临时文件,它需要一些与您的应用程序通信的方法。 至少它应该能够提供信号量。 然而,这样做的复杂性比仅让原始应用程序处理其自己的文件的复杂性更大。
The responsibility for cleaning up temporary files ought to be on the application that created them. It is very easy. I use a class that looks something like this:
If you are required to clean up another application's temporary files it needs some means of communicating with yours. At a minimum it should be able to provide a semaphore. However the complexity of doing this is greater than the complexity of just having the originating application take care of its own files.
如果您尝试确定性地清除“临时文件”类型文件夹的内容,则可能会面临删除其他进程正在使用的文件的风险。 Windows 操作系统提供了一些工具,允许用户在卷的可用磁盘空间达到特定阈值时删除这些文件。
现在,如果您可以确定在使用特定临时文件后将不再需要它,那么删除该特定文件没有任何缺点。
If you attempt to deterministically clear the contents of a Temporary Files type folder, you risk removing files that are in use by other processes. The Windows operating system provides tools to allow users to remove those files when the volume's available disk space reaches a certain threshold.
Now, if you can determine that after you use a specific temporary file that it will no longer be needed, then there's no down-side to deleting that specific file.
我们有一个类似的文件生命周期问题 - (winforms) 应用程序可以从中央存储库存储中获取文档供用户查看,但用户应该能够关闭我们的应用程序,而不会影响他们正在查看的文件。 出于安全考虑,需要清理文档文件,以免它们放置太久。
我们使用的解决方案是在用户的独立存储区域中创建一个具有日期派生名称的文件夹,并将其用作今天的缓存。 在应用程序关闭和启动时,我们检查以前过期的缓存文件夹并删除它们(和内容)。
We have a similar file lifetime problem - the (winforms) application can fetch a document from a central repository store for the user to view, but the user should be able to close our app without it affecting the file they are viewing. Due to security concerns the document files need to be cleaned up so they aren't lying around too long.
The solution we use is to create a folder with a date-derived name in the users' isolated storage area and use that as today's cache. At application close-down and start-up we check for previous dated cache folders and delete them (and contents).
我相信当用户注销时它们会被删除。
在人们每天关闭电脑的时代,这可能是一个完美的解决方案。
现在,人们可能几个月都不会注销,所以依赖它可能不是一个好主意。
此外,如果他们在没有关闭的情况下关闭计算机,那么文件也不会被删除。
您可以使用文件系统观察程序来监视您写入的文件,并等待一段时间不活动(10 分钟、1 小时、1 天或任何时间),然后删除该文件。
但这并不适用于所有情况。 某些程序可能“打开”文件,但可能没有锁定底层文件。 如果发生这种情况,您将无法知道何时可以安全删除该文件。
但是,我认为您可能可以忽略这个问题。 无论如何,大多数人可能都有多余的硬盘空间,因此他们可能不太可能遇到它。 如果他们这样做,我认为 Windows 会弹出一个磁盘空间不足的对话框,让他们可以选择清除临时目录,这将解决问题。
更新:
我认为 Windows Update 大约每月会重新启动用户的计算机一次,因此发生这种情况时应清除临时文件。
不能保证不会出现问题,但实际上我认为这种情况应该很少见。
更新2:
在评论的提示下,我去实际测试了这个,看起来Windows在用户注销时不会删除临时文件。 然而,我仍然说,就OP的情况而言,这不是一个真正的问题。 当它成为问题时(因为磁盘空间不足)Windows 将提示用户删除临时文件。
I believe they will be deleted when the user logs out.
Back in the days when people turned off their pcs every day, that was probably a perfect solution.
Now, people can potentially go months without logging off, so it may not be a good idea to rely on that.
Also, if they turn their machine off without shutting down then the files won't get deleted either.
You could use a file system watcher that watches the file you write, and wait for some period of inactivity (10 minuites, 1 hour, 1 day, or what ever) and then delete the file.
That won't work for everything though. Some programs may have a file "open", but might not have the underlying file locked. If that happens, then you would have no way of knowing when it's safe to delete the file.
However, I'm thinking you can probably just ignore the problem. Most people probably have a surplus of hard drive space anyways, so they probably aren't likely to run into it. If they do, I think Windows pops up a low disk space dialog that gives them the option to clear their temp directory, which would solve the problem.
Update:
I think Windows Update will restart the user's computer about once a month, so the temp files should be cleared when that happens.
There's no guarantee that there won't be problems, but in practice I think it should be pretty rare.
Update 2:
Prompted by a comment, I went and actually tested this, and it looks like windows doesn't delete the temp files when the user logs out. However, I still say that in the OP's case, this isn't a real problem. When it becomes a problem (because disk space is low) Windows will prompt the user to delete temporary files.
在 ASP.NET 中使用临时文件时,请使用 PAGE_DISPOSED 事件删除在页面中创建的任何临时文件。 否则,您最终可能会与组件发生争用,这些组件在 DISPOSED 之前不会释放。
When using temporary files in ASP.NET, use the PAGE_DISPOSED event to delete any temporary files you created in the page. Otherwise you may end up with contention with components that do not let go until DISPOSED.
我使用这个解决方案(非常可靠):
每次需要临时文件时,请使用:
为了确保在应用程序关闭或崩溃后将所有临时文件删除,放在
应用程序启动时。
I use this solution (quite reliable):
Every time you need temporary file use:
To be sure all temporary files are deleted after application closes or crashes put
at start of the application.
您可以创建一个临时文件,您可以在其中继续写入处理过程中创建的文件名,在处理结束时您可以(前提条件 - 实现 IDisposable 并)操作完成后在 Disponse 方法中删除。
其他方式是编写独立进程,每个时间间隔执行清理。
在文件末尾附加日期,并在当天结束时删除该日期,进程将生成末尾带有新日期的新文件。
在
divo tempfilecollection 类给出的一个好建议。
希望能帮助到你。
you can create one temp file, in which you can keep writing the file names created during the process, at the end of processing may be you can (precondition - Implement IDisposable and) delete in the Disponse method after operation is completed.
Other way is write independent process which perform cleanup every interval.
Attache the date at the end of the file, and delete at the end of the day with that date, and process will generate new files wiht new date at the end.
one good suggestion given by divo tempfilecollection class.
hope it helps.