Question File is being used by another process?

crystaluz

Well-known member
Joined
Feb 17, 2009
Messages
55
Programming Experience
Beginner
Hello there...

I have this function to compress a folder :

Sub compressFolder()

        Dim folderToCompress As String
        Dim newDirectory As String = "Compressed"
        Dim compressedLocation As String
        compressedLocation = My.Settings.compressedLocation + "\" + "The Seeker.zip"


        folderToCompress = My.Settings.imageLocation

        If My.Computer.FileSystem.FileExists(compressedLocation) Then

            My.Computer.FileSystem.DeleteFile(compressedLocation)

        End If

        ZipFile.CreateFromDirectory(folderToCompress, compressedLocation)

    End Sub



And this function to e-mail the compressed file:

Sub sendMailAttachment()

        Try

            Dim fileAttached As System.Net.Mail.Attachment

            Dim SMTP_Server As New SmtpClient
            Dim e_mail As New MailMessage()

            SMTP_Server.UseDefaultCredentials = False
            SMTP_Server.Credentials = New Net.NetworkCredential("blablabla@mail.com", "asdasdasd")
            SMTP_Server.Port = 587
            SMTP_Server.EnableSsl = True
            SMTP_Server.Host = "smtp.gmail.com"

            e_mail = New MailMessage()
            e_mail.From = New MailAddress("asdasdasda@gmail.com")
            e_mail.To.Add("asdasdasdas@live.com")
            e_mail.Subject = "The Seeker Captured Images"
            e_mail.IsBodyHtml = False
            e_mail.Body = "Gotcha!"

            fileAttached = New System.Net.Mail.Attachment(My.Settings.compressedLocation + "\" + "The Seeker.zip")
            Dim disposition As ContentDisposition = fileAttached.ContentDisposition
            disposition.CreationDate = IO.File.GetCreationTime(My.Settings.compressedLocation + "\" + "The Seeker.zip")
            disposition.ModificationDate = IO.File.GetLastWriteTime(My.Settings.compressedLocation + "\" + "The Seeker.zip")
            disposition.ReadDate = IO.File.GetLastAccessTime(My.Settings.compressedLocation + "\" + "The Seeker.zip")
            e_mail.Attachments.Add(fileAttached)
            SMTP_Server.Send(e_mail)
            MsgBox("Sent!")

        Catch error_T As Exception

            MsgBox(error_T.ToString)

        End Try

    End Sub



This event when send button is clicked :

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        compressFolder()
        sendMailAttachment()

    End Sub


The code works perfectly when the button is clicked for the first time. But when I click it for the second time, it says "The process cannot access the file because it is being used by another process". It stopped debugging at line 13 in function compressFolder().

Any help and explanation? Thank you :)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
If you had read the documentation as you should have done then you'd know that you have to dispose your Attachment object to release the file. The file has to be opened to be read and attached and it's not closed until you dispose.
 
Top Bottom