C# technique for deleting a file from web server immediately after closing
I'm creating PDF invoices in a C# MVC application that I'm attaching to an email and sending, ideally, once the email is sent I'd like to delete the invoice to free up server space and improve privacy/security. I've written code to do it but it fails 50% of the time because the file is locked by another process (I'm not sure if it's the create/write process that's locking it or the email send). I'm sending the email asynchronously (i.e. the code to delete doesn't execute until the email has sent).

I'd appreciate some tips on how to handle this problem. I could run a job to clean up old files but I'd prefer to clean them up as I go...
C# technique for deleting a file from web server immediately after closing
===

I'm creating PDF invoices in a C# MVC application that I'm attaching to an email and sending, ideally, once the email is sent I'd like to delete the invoice to free up server space and improve privacy/security. I've written code to do it but it fails 50% of the time because the file is locked by another process (I'm not sure if it's the create/write process that's locking it or the email send). I'm sending the email asynchronously (i.e. the code to delete doesn't execute until the email has sent).

I'd appreciate some tips on how to handle this problem. I could run a job to clean up old files but I'd prefer to clean them up as I go...