views:

75

answers:

2

Hi,

I'm in the need again to manage a temporary folder where parts of our application store documents, e.g. between printing and importing to a dms.

Those files should be deleted on application shutdown and ideally on application start as well, just in case something went wrong.

I just thought of a simple class implementing IDisposable that can be used inside Main() with a using()-statement, but somehow this feels dirty. Using Directory.Delete(path, true); inside a catch block that catches all IOExceptions didn't really worked reliably in past.

Any opinions on how to implement such a feature the smart way? Any recommendendations?

The actual path to use is not relevant to us, but we do use %AppData%\[Programname]\Temp now.

Is it possible to create really temporary files on Windows which are deleted on shutdown?

thx for your time.

A: 

I don't see an issue with using the IDisposable approach. Another alternative would be to use a try, finally block in the shut-down area of your code and insert the cleanup code in the finally block, to ensure it executes. The only issue is if you are using a multi-threaded app then you might want to setup the AppDomainUnhandledException event handler.

Matt
My feeling to dirty code belongs on just using Directory.Delete(path, true); and that hope on working anyway...
BeowulfOF
A: 

Just a notice. Windows has special folders for temp files: one in the user's profile and one \WINDOWS\Temp.

You see, if your program not removed temp files for any reason the user will be able to do this with windows Disk Cleanup tool.

It looks like windows doesn't have self-cleaned temp like in *nix. But I've found http://www.tek-tips.com/viewthread.cfm?qid=1314337&page=1. There people suggest to write custom script that will be executed on LogOff. Hope this helps.

Trickster