Thursday, September 10, 2009

Keep clean with temporary files

Every developer runs into a situation where temporary files are needed. In cause of an error, many developers forget to clean up this garbage within the temporary files directory. And I HATE that.

So therefore here is an hint for C# developers:
Just use the
FileOptions.DeleteOnClose
value as parameter of the
FileStream
constructor, this will keep you handles clean.

Heres comes an excample:
using (FileStream fStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose)))
{
// do work
}

Good luck.

No comments:

Post a Comment