Possible Duplicate:
delete attachment file
I am trying to delete a file automatically after it's being send with the following code:
protected void btnSend_Click(object sender, EventArgs e)
{
// Inserting attachment to the email
using (Attachment data = new Attachment("C:\\local\\vCardGenerator.Website\\" + "FirstName_LastName.vcf", MediaTypeNames.Application.Octet))
{
// add Send E-mail class
SendvCard smtp = new SendvCard();
// Calls method to class
smtp.MailvCard("anonymous@domain.com", "C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
}
// Status label + Delete file
lblStatus.Text = "vCard Send to:" + " " + txtMail.Text;
//Delete file after being send as an attachment with the mail
FileInfo DeleteFileInfo = new FileInfo("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
if (DeleteFileInfo.Exists)
File.Delete("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
Debugging without the 'auto delete' runs perfectly smooth, it even sends the email with the attachment, but when I try to delete the attachment after it's being send I get the following error pop-up:
The process cannot access the file. (~\"Path") because it is being used by another process.
Is there anyone that knows why this error may occur?
Do I need to Dispose the file first?
Willing to provide any other/more information if needed.
Thanks in Advance,