简体   繁体   English

临时文件夹中的文件会自动删除吗?

[英]Are files in the temporary folder automatically deleted?

如果我使用Path.GetTempPath()创建一些文件 - 它会在某个阶段自动被删除,还是由我来删除它?

FileOptions.DeleteOnClose will cause the file to be deleted automatically when closed. FileOptions.DeleteOnClose将导致文件在关闭时自动删除。 This also works if the program is terminated by an exception.如果程序被异常终止,这也有效。

For example, as mentioned in this answer :例如,如this answer中所述:

using (FileStream fs = new FileStream(Path.GetTempPath() + "foo.bar",
       FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
       4096, FileOptions.RandomAccess | FileOptions.DeleteOnClose))
{
    // temp file exists
}

// temp file is gone

No, you will need to manually delete the file.不,您需要手动删除该文件。 Path.GetTempPath() just gives you the folder path to the temp folder. Path.GetTempPath()只是为您提供临时文件夹的文件夹路径。

Starting with Windows 10, the answer is posibly yes - depending on the machine configuration and the amount of free space on the drive hosting the TEMP folder.从 Windows 10 开始,答案可能是肯定的 - 取决于机器配置和托管 TEMP 文件夹的驱动器上的可用空间量。

Specifically, Storage Sense can arbitrarily delete files from the TEMP folder (I found that out the hard way) if enabled by the user.具体来说,如果用户启用, Storage Sense可以从 TEMP 文件夹中任意删除文件(我发现很难)。 And from what I can tell, it will self-enable on low disk space.据我所知,它会在磁盘空间不足时自动启用

Basically if your application does not delete a file it will still be there until your application removes it and you should manage files your app creates based on that idea.基本上,如果您的应用程序不删除文件,它仍然会存在,直到您的应用程序将其删除,您应该根据该想法管理您的应用程序创建的文件。

That said, once the file is closed you must always allow for the fact that it may not be there next time you want it and that you may need to recreate it.也就是说,一旦文件关闭,您必须始终考虑到下次您想要它时它可能不存在并且您可能需要重新创建它的事实。 For example, Windows has a "disk cleanup tool" which may be run when space gets low, when directed by a user, or on a schedule...例如,Windows 有一个“磁盘清理工具”,它可以在空间不足、用户指示或按计划运行时运行……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM