views:

63

answers:

2

My program writes temporary PDF files (using Path.GetTempFileName) which are then passed to the default PDF handler (typically Adobe Reader) for display. I have a finally block in my Main method to delete the temp files, but if Adobe Reader is still open when my program closes, the files remain open and can't be deleted.

One solution would just be to leave them there and assume the user will clear out his temporary folder someday; the files are only 1.5 MB each. But is there a better way?

I thought I once read about a way to register files to be deleted on the next reboot, similar to what installers and Windows Update do, but I can't find any information on it now. Is there a function like that I can use here?

A: 

Can you register or store the file(s) opened, and then next time your app starts try delete the temporary PDF files? So make your app take care of the tidy up next time around. The user should have closed it by then.

Ciaran Archer
+2  A: 

See the KB article How To Move Files That Are Currently in Use. As outlined there, you'll need to use a different method on NT (which includes 2000, XP, Vista and Windows 7) and for legacy OS versions, if your app still runs on those.

Pinvoke.net has managed definitions for the Win32 API calls and constants involved.

mdb