如何正确使用应用程序中的临时存储
我再次需要管理一个临时文件夹,其中部分应用程序存储文档,例如在打印和导入到 dms 之间。
这些文件应该在应用程序关闭时删除,最好在应用程序启动时也删除,以防出现问题。
我只是想到了一个实现 IDisposable 的简单类,可以在 Main() 中通过 using() 语句使用它,但不知怎的,这感觉很脏。在捕获所有 IOException
的 catch 块中使用 Directory.Delete(path, true);
过去并没有真正可靠地工作。
关于如何以智能方式实现这样的功能有什么意见吗?有什么推荐吗?
实际使用的路径与我们无关,但我们现在使用 %AppData%\[Programname]\Temp
。
是否可以在 Windows 上创建临时文件并在关机时删除这些文件?
谢谢您的宝贵时间。
I'm in the need again to manage a temporary folder where parts of our application store documents, e.g. between printing and importing to a dms.
Those files should be deleted on application shutdown and ideally on application start as well, just in case something went wrong.
I just thought of a simple class implementing IDisposable
that can be used inside Main() with a using()-statement, but somehow this feels dirty. Using Directory.Delete(path, true);
inside a catch block that catches all IOException
s didn't really worked reliably in past.
Any opinions on how to implement such a feature the smart way? Any recommendendations?
The actual path to use is not relevant to us, but we do use %AppData%\[Programname]\Temp
now.
Is it possible to create really temporary files on Windows which are deleted on shutdown?
thx for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为使用 IDisposable 方法没有问题。另一种选择是在代码的关闭区域使用 try,finally 块,并将清理代码插入到 finally 块中,以确保其执行。唯一的问题是,如果您使用多线程应用程序,那么您可能需要设置 AppDomainUnhandledException 事件处理程序。
I don't see an issue with using the IDisposable approach. Another alternative would be to use a try, finally block in the shut-down area of your code and insert the cleanup code in the finally block, to ensure it executes. The only issue is if you are using a multi-threaded app then you might want to setup the AppDomainUnhandledException event handler.
只是一个通知。 Windows 有专门的临时文件文件夹:一个位于用户配置文件中,另一个位于\WINDOWS\Temp。
您会看到,如果您的程序因任何原因未删除临时文件,用户将能够使用 Windows 磁盘清理工具来执行此操作。
看起来 Windows 没有像 *nix 那样的自清洁温度。
但我发现 http://www.tek-tips .com/viewthread.cfm?qid=1314337&page=1。人们建议编写将在注销时执行的自定义脚本。希望这有帮助。
Just a notice. Windows has special folders for temp files: one in the user's profile and one \WINDOWS\Temp.
You see, if your program not removed temp files for any reason the user will be able to do this with windows Disk Cleanup tool.
It looks like windows doesn't have self-cleaned temp like in *nix.
But I've found http://www.tek-tips.com/viewthread.cfm?qid=1314337&page=1. There people suggest to write custom script that will be executed on LogOff. Hope this helps.