windows 临时文件夹中的文件会自动删除吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2262175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are files in the temporary folder automatically deleted?
提问by Yaron Naveh
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?
如果我使用 Path.GetTempPath() 创建一些文件 - 它会在某个阶段自动被删除,还是由我来删除它?
采纳答案by Jason Evans
No, you will need to manually delete the file. Path.GetTempPath() just gives you the folder path to the temp folder.
不,您需要手动删除该文件。Path.GetTempPath() 只是为您提供临时文件夹的文件夹路径。
回答by finnw
FileOptions.DeleteOnClose
will cause the file to be deleted automatically when closed. This also works if the program is terminated by an exception.
FileOptions.DeleteOnClose
将导致文件在关闭时自动删除。如果程序因异常终止,这也有效。
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
回答by Monosodium
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 有一个“磁盘清理工具”,它可以在空间不足、由用户指导或按计划运行时运行......