临时文件Windows 上的文件夹 [英] Temporary files & folders on Windows

查看:38
本文介绍了临时文件Windows 上的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过多次加载 DLL 来模拟多线程(它是为此而设计的).由于LoadLibrary() 并没有真正允许它,因此DLL 通过GetTempPath()GetTempFileName() 将自身复制到一个临时文件中.

Emulating multithreading by loading a DLL multiple times (it's designed for this). Since LoadLibrary() doesn't really allow it, the DLL copies itself into a temporary file, via GetTempPath() and GetTempFileName().

这是一个小文件,在线程销毁时它会释放库并删除临时文件.现在的问题是线程的数量是可配置的(疯子可以选择 50、100、更多),这基本上暴露了运行不稳定、崩溃的风险,而不是通过通常的删除临时文件"例程.

It's a small file and upon thread destruction it frees the library and deletes the temporary file. Problem now is that the number of threads is configurable (maniacs can pick 50, 100, more) which basically exposes the risk of running unstable, crash and not go through the usual "delete temporary files" routine.

如果我把这些临时文件留在那里死了可以吗?操作系统通常会自行清理吗?或者我应该写一个自动清理程序?如果是,我该如何保存另一个临时文件以保存包含这些文件的列表,而不是遇到 UAC 限制或其他问题?

Is it okay if I just leave those temporary files to die there? Does the OS usually clean the up by itself? Or should I write an autocleanup routine? If yes, how can I go about saving another temporary file to hold a list with those files, and not hit UAC restrictions or otherwise?

有什么想法吗?

推荐答案

您的 LoadLibrary/多线程讨论让我感到困惑,但没关系.

Your LoadLibrary/multithreaded discussion leaves me confused, but that's ok.

Windows 上的临时文件通常不会被创建它们的进程以外的任何东西删除.如果用户运行磁盘清理"实用程序或类似工具,他可能能够为他自动删除这些文件.我不认为这种情况经常发生.

Temp files on Windows don't generally get deleted by anything other than the process that created them. If the user runs the "Disk Cleanup" util or similar tool, he might be able to get those files automatically deleted for him. I don't think that happens too often.

最佳方法.让您的应用程序在 temp 文件夹(可能还有每个流程实例的子文件夹)中创建一个子目录.当您的应用程序退出时,它会通过 DeleteFile 清理它自己的一组临时文件.在应用重新启动时,它可以有一些逻辑来清理由于之前的进程崩溃而仍然存在的文件夹和文件.

Best approach. Have your application create a sub-directory within the temp folder (and possibly subfolders for each process instance). When you application exits, it cleans up it's own set of temp files via DeleteFile. On app re-start it can have some logic to cleanup folders and files still lingering around as a result of a previous process crashing.

您还可以考虑在 CreateFile 上使用 FILE_ATTRIBUTE_TEMPORARY 标志.

You could also look at using the FILE_ATTRIBUTE_TEMPORARY flag on CreateFile.

您还可以考虑为您的流程设置一个全局异常处理程序.处理程序会在崩溃退出之前删除它自己的临时目录.

You could also consider having a global exception handler for your process. The handler would delete it's own temp directory before exiting on a crash.

这篇关于临时文件Windows 上的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆