我的代码处理一个网站上的信息,然后将其导出为word文档,然后将其转换为PDF,并继续将其附加到电子邮件代码上。我已经设法用PDF发送了电子邮件,但在文件管理方面有问题。在这种情况下,我想要删除文件夹中的文件,即生成的word和PDF文档。代码运行并能够删除word文档,但无法删除PDF文档。
当我手动尝试删除文件时,错误是"该操作无法完成,因为文件在webdev.webserver40.exe中打开"。当前在调试模式下运行,希望将来在IIS上运行。
下面是我的电子邮件代码片段
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
e_mail = New MailMessage()
e_mail.Subject = "Job Completed "
e_mail.IsBodyHtml = False
e_mail.Body = msg
e_mail.Attachments.Add(New Attachment(Server.MapPath("PDF" + filename + ".pdf")))
Smtp_Server.Send(e_mail)
Smtp_Server.Dispose()
e_mail.Attachments.Clear()
e_mail.Dispose()
删除认证和发送地址。下面是删除文件
的代码Dim filePaths As String() = Directory.GetFiles(HttpContext.Current.Server.MapPath("PDF"))
For Each filePath As String In filePaths
File.Delete(filePath)
Next
下面的是用于转换pdf
的类文件。 Imports Microsoft.VisualBasic
Imports System.IO
Imports Microsoft.Office.Interop.Word
Public Class ConvertWordToPDF
Public Sub convertToPDF(strFileName As String)
' Create a new Microsoft Word application object
Dim word As New Microsoft.Office.Interop.Word.Application()
' C# doesn't have optional arguments so we'll need a dummy value
Dim oMissing As Object = System.Reflection.Missing.Value
' Get list of Word files in specified directory
Dim dirInfo As New DirectoryInfo(HttpContext.Current.Server.MapPath("PDF"))
Dim wordFiles As FileInfo() = dirInfo.GetFiles("*.docx")
word.Visible = False
word.ScreenUpdating = False
' Cast as Object for word Open method
Dim filename As [Object] = DirectCast(wordFiles(0).FullName, [Object])
'Dim fileName As Object = strFileName
' Use the dummy value as a placeholder for optional arguments
Dim doc As Document = word.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, _
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
oMissing, oMissing, oMissing, oMissing)
doc.Activate()
Dim outputFileName As Object = wordFiles(0).FullName.Replace(".docx", ".pdf")
Dim fileFormat As Object = WdSaveFormat.wdFormatPDF
' Save document into PDF Format
doc.SaveAs(outputFileName, fileFormat, oMissing, oMissing, oMissing, oMissing, _
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
oMissing, oMissing, oMissing, oMissing)
' Close the Word document, but leave the Word application open.
' doc has to be cast to type _Document so that it will find the
' correct Close method.
Dim saveChanges As Object = WdSaveOptions.wdDoNotSaveChanges
DirectCast(doc, _Document).Close(saveChanges, oMissing, oMissing)
doc = Nothing
' word has to be cast to type _Application so that it will find
' the correct Quit method.
DirectCast(word, _Application).Quit(oMissing, oMissing, oMissing)
word = Nothing
End Sub
End Class
编辑:添加了检查文件是否被锁定的代码
Protected Overridable Function IsFileLocked(file As FileInfo) As Boolean
Dim stream As FileStream = Nothing
Try
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
Catch generatedExceptionName As IOException
'the file is unavailable because it is:
'still being written to
'or being processed by another thread
'or does not exist (has already been processed)
Return True
Finally
If stream IsNot Nothing Then
stream.Close()
End If
End Try
'file is not locked
Return False
End Function
在创建PDF后返回false。
刚刚测试了一个能够重现问题的场景。我能够通过将附件添加为流而不仅仅是文件的路径来修复它。只要记住以这样或那样的方式关闭流。
这段代码给出了你描述的错误:
Dim smtp As New SmtpClient("...")
message.Attachments.Add(New Attachment("c:myfile.txt"))
smtp.Send(message)
File.Delete("c:myfile.txt")
如此:
Dim smtp As New SmtpClient("...")
Using reader As New StreamReader("C:myfile.txt")
Dim a As New Attachment(reader.BaseStream, "myfile.txt")
message.Attachments.Add(a)
smtp.Send(message)
End Using
File.Delete("c:myfile.txt")