Forum Moderators: open

Message Too Old, No Replies

.net 2.0 file upload and email

         

TheSeoGuy

4:07 pm on Nov 28, 2006 (gmt 0)

10+ Year Member



I have a page that allows for a file upload. The code then takes that file, attaches it to an e-mail and send's the e-mail off. This all works exactly as it should. However, after the e-mail is sent off, I would ideally like to remove the previously uploaded file from the server...

Everytime I try this I get the following error:
The process cannot access the file '<filepath>' because it is being used by another process.

Even when I attempt to delete the file through an ftp client, I get this error.

My code snippet follows ---------------------
'--- Get the uploaded resume file.
Dim fileName As String = GetFileName(resumeFileUpload.PostedFile.FileName)

Try
resumeFileUpload.PostedFile.SaveAs("C:\PATH GOES HERE" + fileName)

Dim mailAttachment = New Attachment("C:\PATH GOES HERE" + fileName)

mailMessage.Attachments.Add(mailAttachment)
mailSmtp.Send(mailMessage)

Catch Exp As Exception
errorLabel.text = "We're sorry, but there was an error."

Finally

System.IO.File.Delete("C:\PATH GOES HERE" + fileName)

End Try

Code Snippet Ends -----------------------------

I would greatly appreciate any help with this issue.

TheSeoGuy

5:16 pm on Nov 28, 2006 (gmt 0)

10+ Year Member



Ok, I finally figured this out...

I was trying to delete the uploaded file, but it was locked and being used by MailMessage object.

Just before my delete statement, I added the following:
mailMessage.Dispose()

And the file is unlocked and deleted from the system.

I hope this is of help to someone...