I have function to create a pdf and then send it to through mail in attachments.
The function to create pdf:
public string CreatePdf() { try { using (document = new Document()) { if (File.Exists(filePath)) { workStream.Dispose(); File.Delete(filePath); } // LOGIC to Create PDF return filePath; } } catch (Exception) { throw; } finally { document.Close(); document.Dispose(); workStream.Close(); } } To add to attachments:
myMail.attachment = new Attachment(new CreatePdf()); When i create file for the first time it is created fine, but when i try to create pdf again i get the following error on File.Delete(filePath)
The process cannot access the file because it is being used by another process. I saw other similar questions but couldn't figure out what needs to be closed exactly as i have closed everything.