I am creating a WPF/C# app that deletes files and creates new ones before closing (from online data). The problem comes when the app randomly crashes before the new file creation process is complete or even starts. 

Is there a way of file recovery (to be done within the app not just undelete apps etc) that I can use in my code so when the app crashes the file that was deleted cannot be lost? 

To be more accurate I delete the 4 files at the start of the app and create new ones at the end of the app. But if the app crashes in the middle the files are lost. 

How can I prevent this issue? 

I tried to reduce the probability of random crashing but once in a while it still happens.