Results 1 to 6 of 6

Thread: [RESOLVED] system.IO.IOException

  1. #1

    Thread Starter
    Lively Member viper5646's Avatar
    Join Date
    Feb 2009
    Location
    kitchener Ontario
    Posts
    90

    Resolved [RESOLVED] system.IO.IOException

    Hi all
    In the following Sub I'm sending an Attachment when the email has been sent I would like to delete the attachment but I get an system.IO.IOException Error. the file I'm trying to delete is being used by another process.
    How can I detect if the process has finished.

    Thanks
    Happy New Year to everyone

    Code:
    Private Sub SendMail()
            Try
                Dim cfiAttach As Net.Mail.Attachment
                Dim Attacmentfile As String = AttDir & fileName & ".pdf"
                Dim SmtpServer As New SmtpClient()
                Dim mail As New MailMessage()
                SmtpServer.Credentials = New Net.NetworkCredential(diaSettings.txtMail_User.Text, diaSettings.txtMail_Pass.Text)
                SmtpServer.Port = smtpPrt
                SmtpServer.Host = SmtpHst
                mail = New MailMessage()
                mail.From = New MailAddress(Mail_user)
                mail.To.Add(Mail_TO)
                ' mail.Bcc.Add(BBC_MailTO)
                mail.Subject = fileName
                mail.Body = ""
                cfiAttach = New Attachment(Attacmentfile)
                If File.Exists(Attacmentfile) Then
                    cfiAttach = New Attachment(Attacmentfile)
                    mail.Attachments.Add(cfiAttach)
                Else
                    MsgBox("Attachment Not Found")
                End If
                SmtpServer.Send(mail)
                MsgBox("Sent")
                System.IO.File.Delete(AttDir & fileName & ".pdf")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    Last edited by viper5646; Dec 29th, 2011 at 11:26 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: system.IO.IOException

    I wonder whether the Send method actually just passes the request off to some Windows process and then returns, so your file is still in use by that process. I don't actually know but I would have thought not. One point to consider is the fact that the Attachment class implements IDisposable and you are not disposing it. That would be the first thing I'd look at, i.e. Dispose your Attachment and see if that releases the file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member viper5646's Avatar
    Join Date
    Feb 2009
    Location
    kitchener Ontario
    Posts
    90

    Re: system.IO.IOException

    Thanks jmcilhinney

    I tried mail.Attachments.IDisposable() and I get the following error Error 'IDisposable' is not a member of 'System.Net.Mail.AttachmentCollection'.
    I also tried mail.Attachments.Disposable I still get system.IO.IOException error.

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: system.IO.IOException

    Jmcilhinney means like this:

    Code:
    Private Sub SendMail()
            Try
                Dim cfiAttach As Net.Mail.Attachment
                Dim Attacmentfile As String = AttDir & fileName & ".pdf"
                Dim SmtpServer As New SmtpClient()
                Dim mail As New MailMessage()
                SmtpServer.Credentials = New Net.NetworkCredential(diaSettings.txtMail_User.Text, diaSettings.txtMail_Pass.Text)
                SmtpServer.Port = smtpPrt
                SmtpServer.Host = SmtpHst
                mail = New MailMessage()
                mail.From = New MailAddress(Mail_user)
                mail.To.Add(Mail_TO)
                ' mail.Bcc.Add(BBC_MailTO)
                mail.Subject = fileName
                mail.Body = ""
                'cfiAttach = New Attachment(Attacmentfile) 'Commented as not necessary
                If File.Exists(Attacmentfile) Then
                    cfiAttach = New Attachment(Attacmentfile)
                    mail.Attachments.Add(cfiAttach)
                Else
                    MsgBox("Attachment Not Found")
                End If
                if cfiAttach isnot nothing then cfiAttach.Dispose
                SmtpServer.Send(mail)
                MsgBox("Sent")
                System.IO.File.Delete(AttDir & fileName & ".pdf")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    Kris

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: system.IO.IOException

    Not quite i00. You can't Dispose the Attachment before calling Send. I do mean call the Dispose method but it must be done AFTER sending the mail. Also, there's no point using an If statement because you know for a fact that the Attachment is not Nothing at that point.

    That said, the preferred way of disposing an short-lived object is with a Using block. The object is created by the Using statement and implicitly disposed at the End Using statement:
    vb.net Code:
    1. Using mailAttachment As New Attachment(filePath)
    2.     'Use mailAttachment here.
    3. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member viper5646's Avatar
    Join Date
    Feb 2009
    Location
    kitchener Ontario
    Posts
    90

    Re: system.IO.IOException

    Thanks jmcilhinney and i00 for your replies.
    There was two calls for the New Attachment(Attacmentfile).
    By deleting the first one and using mail.Attachments.Dispose()
    did the trick .

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width