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

发布于 2024-08-21 16:32:50 字数 61 浏览 8 评论 0原文

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

If I create some file using Path.GetTempPath() - does it automatically get deleted at some stage, or is it up to me to delete it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-08-28 16:32:50

FileOptions.DeleteOnClose关闭时会导致文件自动删除。如果程序因异常而终止,这也适用。

例如,正如这个答案中提到的:

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

FileOptions.DeleteOnClose will cause the file to be deleted automatically when closed. This also works if the program is terminated by an exception.

For example, as mentioned in 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
痴意少年 2024-08-28 16:32:50

不,您需要手动删除该文件。 Path.GetTempPath() 只是为您提供临时文件夹的文件夹路径。

No, you will need to manually delete the file. Path.GetTempPath() just gives you the folder path to the temp folder.

紫竹語嫣☆ 2024-08-28 16:32:50

从 Windows 10 开始,答案是可能 - 取决于计算机配置和托管 TEMP 文件夹的驱动器上的可用空间量。

具体来说,存储感知<如果用户启用了 /a> ,则可以从 TEMP 文件夹中任意删除文件(我发现这是很困难的)。据我所知,它将 在磁盘空间不足时自行启用

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.

Specifically, Storage Sense can arbitrarily delete files from the TEMP folder (I found that out the hard way) if enabled by the user. And from what I can tell, it will self-enable on low disk space.

落日海湾 2024-08-28 16:32:50

基本上,如果您的应用程序不删除文件,它仍然会在那里,直到您的应用程序删除它,并且您应该根据该想法管理您的应用程序创建的文件。

也就是说,一旦文件关闭,您必须始终考虑到这样一个事实:下次您需要它时它可能不存在,并且您可能需要重新创建它。例如,Windows 有一个“磁盘清理工具”,可以在空间不足时、在用户指示下或按计划运行...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文