views:

225

answers:

4

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?

+6  A: 

Hi there.

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

Cheers. Jas.

Jason Evans
A: 

if you use Path.GetTempFileName() the temporary file will indeed be removed by windows the next time the machine is rebooted.

HuwR
I don't believe this is correct. From the documentation: "Temporary files whose names have been created by this function are not automatically deleted. To delete these files call DeleteFile."
TrueWill
I have way too many files in my temp directory for this to be correct. You have to delete the files.
Arve
A: 

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

Monosodium
+3  A: 

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

finnw