0
votes

Here is the situation:

Asp.Net Web Forms site using Open XML to read in a (via a stream) word document (docx). I then insert some text into the document and then write the file back out to a different location. It is then emailed to an end user. All of this works great.

The problem i am running into is that I can't the new file written by the site. I receive the following error: "The process cannot access the file (file name here) because it is being used nt another process"

I have confirmed that it is the site (or IIS) that is holding on to the file.

Here is the code that reads the original file and generates the new file:

    Private Function GetDocument(worddoc As String) As Integer
    Dim byteArray As Byte() = File.ReadAllBytes("\\WEB-DEV-1\HR_Documents\" & worddoc)
    Using Stream As New MemoryStream()
        Stream.Write(byteArray, 0, CInt(byteArray.Length))
        Try
            'Set Row & Cell variables
            Dim rowNum As Integer = 0
            Dim cellNum As Integer = 0
            'Set File Stream
            Using doc As WordprocessingDocument = WordprocessingDocument.Open(Stream, True)
                'Employee Name Insert
                'Find first table in document
                Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
                'First Row in tbl
                Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
                'Find first cell in row
                Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
                'Insert selected Employee Name
                Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
                Dim r As Run = p.Elements(Of Run)().First()
                Dim txt As Text = r.Elements(Of Text)().First()
                txt.Text = "Employee Name: " & ddlEmployeeList.SelectedItem.Text

                'Supervisor Name Insert
                'Check for form
                If ddlFormChoice.SelectedIndex <> 2 Then
                    'Reset row to supervisors row in table
                    row = tbl1.Elements(Of TableRow)().ElementAt(1)
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    'Reset row to supervisors row in table
                    row = tbl1.Elements(Of TableRow)().ElementAt(2)
                End If
                If ddlFormChoice.SelectedIndex <> 2 Then
                    'Reset cell to supervisor cell in row
                    cell = row.Elements(Of TableCell)().ElementAt(1)
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    'Reset cell to supervisor cell in row
                    cell = row.Elements(Of TableCell)().ElementAt(0)
                End If

                'Insert selected Employee Name
                p = cell.Elements(Of Paragraph)().First()
                r = p.Elements(Of Run)().First()
                txt = r.Elements(Of Text)().First()
                If ddlFormChoice.SelectedIndex <> 2 Then
                    txt.Text = "Supervisor: " & ddlSupervisorList.SelectedItem.Text
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    txt.Text = "Manager/Supervisor: " & ddlSupervisorList.SelectedItem.Text
                End If
                doc.Close()
            End Using
            'Save File to temp location
            File.WriteAllBytes("\\WEB-DEV-1\HR_Documents\TempDocs\" & worddoc, Stream.ToArray())
            Stream.Close()
            Stream.Dispose()
            Return 1
        Catch ex As Exception
            Return Nothing
        End Try
    End Using
End Function

I close the OpenXML doc and the stream as well dispose of the stream but when I try to delete the file from the main sub that called the function I get the error listed above.

What am I missing?? I closed the doc, the stream and disposed of the stream. Why is the site still holding the file?

Note here the line of code that trys to delete the file;

File.Delete("\\Web-Dev-1\HR_Documents\TempDocs\" & fileAttach)
1

1 Answers

0
votes

So after most of the day i finally found out what the problem was. After the document was created, saved , and emailed it was being held by the email method. For some reason i thought that when the method finishes that it disposed of the Mail Message but this not the case.

Once I added the dispose line it all worked fine.

Only been Googling for almost two days. :|