How to delete temporary files that are still open in other programs?

My program writes temporary PDF files (using Path.GetTempFileName), which are then passed to the default PDF handler (usually Adobe Reader) for display. I have a block finallyin my main method for deleting temporary files, but if Adobe Reader is still open when my program closes, the files remain open and cannot be deleted.

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

I thought that I once read about the method of registering files that will be deleted the next time you reboot, similar to what installers and Windows Update do, but I can not find any information about it now. Is there a function that I can use here?

+3
source share
2 answers

See KB article How to Move Files That Are Currently Used . As indicated there, you will need to use a different method for NT (which includes 2000, XP, Vista, and Windows 7) and for older versions of the OS if your application still runs on them.

Pinvoke.net manages Win32 API call and constant definitions.

+2

, PDF? , . .

0

Source: https://habr.com/ru/post/1752723/


All Articles