既然 Storage Sense 可以随时清理它,为什么还要使用 %TEMP% 文件夹? [英] Why would one ever use the %TEMP% folder now that Storage Sense can clean it at any time?

查看:44
本文介绍了既然 Storage Sense 可以随时清理它,为什么还要使用 %TEMP% 文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Windows 10 开始,Storage Sense 允许用户指定频率为每天一次的 %TEMP% 文件夹清理.从技术上讲,它可以更频繁地运行并设置为在低磁盘空间上激活,具体取决于一个人的磁盘使用模式.

Starting with Windows 10, Storage Sense has allowed users to specify %TEMP% folder cleanup that are as frequent as once a day. Technically it can run even more often is set to activate on low disk space, depending on one's disk usage patterns.

有鉴于此,%TEMP% 文件夹的意义何在?我将如何使用一个文件夹,其中我放置在那里的每个文件在我完成写入后的那一刻在技术上都可以被系统删除?

In light of that, what is the point of the %TEMP% folder? How would I ever use a folder where every file I put there can technically be removed by the system the moment after I finish writing it?

这是一个真实世界的场景,这让我感到震惊(为简洁起见,代码已简化):

Here is a real world scenario where this hit me (code simplified for brevity):

var ffmpegPath = Path.Combine(Path.GetTempPath(), "ffmpeg");
DownloadFfmpeg(path: ffmpegPath); 

foreach (var videoFile in videoFiles) { //suppose there are dozens of files to process
   DoSomeHeavyProcessing(ffmpegPath);   //suppose each file takes an hour to process
}

这在最初的几个小时里效果很好,但随后在某个任意时间点下载的 ffmpeg 文件夹被删除,并且无法处理所有后续文件.事实上,如果我理解正确的话,理论上即使这样的代码也可能会失败:

This worked great for the first few hours, but then at some arbitrary point in time the downloaded ffmpeg folder was deleted and all the subsequent files could not be processed. In fact, if I understand correctly, in theory even code like this could fail:

var path = Path.Combine(Path.GetTempPath(), "foo");
File.WriteAllText(path, "bar");
Console.WriteLine(File.ReadAllText(path));

现在,我知道如何解决这个问题 - 只需使用 %APPDATA%%LOCALAPPDATA%%PROGRAMDATA%.但这就是重点 - 自从 Storage Sense 出现以来,我为什么要使用 %TEMP% 而不是以前的文件夹?

Now, I know how to solve this - simply use %APPDATA%, %LOCALAPPDATA% or %PROGRAMDATA%. But that's the point - since the advent of Storage Sense, why would I ever use %TEMP% rather than the former folders?

推荐答案

%TEMP% 文件夹——顾名思义——用于存放临时文件,这些文件只需要(通常短)一段时间,之后可以删除.在理想的世界中,写入临时文件夹的每个应用程序都会在之后清理并删除它创建的临时文件,当它们不再需要时.但这不会发生,因此 %TEMP% 文件夹往往会变得很大.

The %TEMP% folder is -- as the name suggests -- for temporary files, which are only needed for a (typically short) period of time and can be deleted afterwards. In an ideal world, every app writing to the temp folder would clean up afterwards and delete the temporary files it created, when they are not needed any longer. But that does not happen, thus %TEMP% folders tend to become huge.

您可以通过获取文件锁定来轻松阻止 Storage Sense 删除您仍然需要的文件.只要 %TEMP% 文件夹中的文件正在使用中,它就不会被删除.处理完文件后,您可以释放文件锁,这意味着您不再需要该文件,并且可以在下次运行 Storage Sense 时将其删除.

You can easily prevent Storage Sense from deleting files you still need by acquiring a file lock on that files. As long as a file in the %TEMP% folder is in use, it won't be deleted. Once processing the file has completed, you can free the file lock, which means you won't need the file any more and it can be deleted in the next run of Storage Sense.

这样做的好处是,您的应用程序不需要清理烂摊子".(即临时文件)了.只要需要,就让应用程序锁定它们.锁被释放后(或应用退出,也会释放文件锁),它们会被系统自动删除.

This has the advantage, that your app doesn't need to clean up the "mess" (i.e temporary files) anymore. Just have the app lock them as long as they are needed. After the lock is freed (or the app exits, which will also free the file locks), they will automatically get deleted by the system.

这篇关于既然 Storage Sense 可以随时清理它,为什么还要使用 %TEMP% 文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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