Asp.net 网站赢得';t使用openxml生成发布文件

Asp.net 网站赢得';t使用openxml生成发布文件,asp.net,vb.net,file,openxml,Asp.net,Vb.net,File,Openxml,情况如下: Asp.NETWeb表单站点使用开放式XML读取(通过流)word文档(docx)。然后我在文档中插入一些文本,然后将文件写回另一个位置。然后通过电子邮件发送给最终用户。所有这些都非常有效 我遇到的问题是,我无法使用该站点编写的新文件。我收到以下错误: “进程无法访问该文件(此处为文件名),因为它正被另一进程使用” 我已经确认是网站(或IIS)保存了该文件 以下是读取原始文件并生成新文件的代码: Private Function GetDocument(worddoc As

情况如下:

Asp.NETWeb表单站点使用开放式XML读取(通过流)word文档(docx)。然后我在文档中插入一些文本,然后将文件写回另一个位置。然后通过电子邮件发送给最终用户。所有这些都非常有效

我遇到的问题是,我无法使用该站点编写的新文件。我收到以下错误: “进程无法访问该文件(此处为文件名),因为它正被另一进程使用”

我已经确认是网站(或IIS)保存了该文件

以下是读取原始文件并生成新文件的代码:

    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

因此,经过一天的大部分时间,我终于发现了问题所在。文档创建、保存并通过电子邮件发送后,它将由电子邮件方法保存。出于某种原因,我认为当该方法完成时,它会处理邮件消息,但事实并非如此

一旦我添加了dispose行,一切都很好

只在谷歌上搜索了将近两天|

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