临时文件夹中的文件会自动删除吗?
如果我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FileOptions.DeleteOnClose
关闭时会导致文件自动删除。如果程序因异常而终止,这也适用。例如,正如这个答案中提到的:
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:
不,您需要手动删除该文件。
Path.GetTempPath()
只是为您提供临时文件夹的文件夹路径。No, you will need to manually delete the file.
Path.GetTempPath()
just gives you the folder path to the temp folder.从 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.
基本上,如果您的应用程序不删除文件,它仍然会在那里,直到您的应用程序删除它,并且您应该根据该想法管理您的应用程序创建的文件。
也就是说,一旦文件关闭,您必须始终考虑到这样一个事实:下次您需要它时它可能不存在,并且您可能需要重新创建它。例如,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...