Question : The process cannot access the file ... because it is being used by another process.

I have googled and been reading for hours on this problem for others. I seem to be taking all the steps they recommend and I still cannot get IIS to let go of the process. I've tried putting it in a loop to wait for the release, which I figured would be a dumb idea, because I can log out and back in and five minutes later it's still locked.

The error occurs on the deletion below... But it also occurs if I remove the deletion code and hit this code once, the file exists, and I hit it again to update the file. IIS seems to never release the file. Is there something in my code I can do to release it? I tried deleting the file immediately after creating it and sending it as an email file attachment, and it sitll won't let me delete it. Does the file attachment process do something to hold onto it?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
            Dim ctr As Integer = 0
            Do
                Try
                    '
                    ' 7/7/2010 spell out more...
                    File.Delete(UniquefileName) ' delete it if exists before writing to it...
                   Dim objStreamWriter As StreamWriter
                    objStreamWriter = File.CreateText(UniquefileName)
                    objStreamWriter.Write(strOutput)
                    'Close the stream
                    objStreamWriter.Close()
                    objStreamWriter = Nothing   ' 7/7/2010 set to nothing
                    bSuccess = True
                    Exit Do
                Catch ex As Exception
                    ctr = ctr + 1
                    If ctr = 100 Then
                        SendError("Path: " & strUrl & "<br>Error: " & ex.Message)
                        Exit Do
                    End If
                End Try
            Loop



file attachment code:

Dim mMailMessage As New MailMessage()
            If attachmentFileName <> String.Empty Then
                Dim attachmentfile As New System.Net.Mail.Attachment(attachmentFileName)
                mMailMessage.Attachments.Add(attachmentfile)
            End If

thanks in advance!

Answer : The process cannot access the file ... because it is being used by another process.

Take a look at this: http://msdn.microsoft.com/en-us/library/system.net.mail.attachment_methods.aspx  I think 'dispose' is what you need to release the resource.
Random Solutions  
 
programming4us programming4us