1

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,

Community
  • 1
  • 1
Rafael
  • 715
  • 1
  • 16
  • 34

3 Answers3

4

If you dispose the the mail message it will close resources within it and unlock the files

Bart
  • 406
  • 3
  • 12
1

SendvCard isn't part of the .NET Framework so I can't tell you for sure, however, I would hazard a guess at it internally creating a MailMessage object which will lock down the attachment until it's disposed. Also it looks as though you are unnecessarily creating a new Attachment object because as far as I can see, it doesn't get used.

What you need to do is dispose of your smtp object before you try to delete the attachment, so if SendvCard implements IDisposable your code could look something like:

using (var 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");
James
  • 75,060
  • 17
  • 154
  • 220
0

I used one of the Attachment constructors which doesn't require a file to start with. I wrote to a MemoryStream, rewinded it and then provided that to the Attachment.

After that I received another error, which will be for a new thread.

I thank everybody for their help and answers even though I didn't need them.

Rafael
  • 715
  • 1
  • 16
  • 34
  • Do you even need the `Attachment` instance at all though? You don't use it anywhere in your code? It looks as though `SendvCard` takes an attachment string as a parameter (I may be wrong). – James Nov 02 '12 at 15:57