【问题标题】:Unable to Delete a file无法删除文件
【发布时间】:2012-05-15 13:03:41
【问题描述】:
 Sub Main()
            Try
                Dim output, filename1, filename2, filename3, date1, date2 As String

                'today's final
                output += "Report Dates: " & date1 & " and " & date2
                filename1 = "filename1.doc"
                SaveToFile(output, filename1)


                'today's daily
                output = "Report Dates: " & date1 & " and " & date2
                filename2 = "filename2.doc"
                SaveToFile(output, filename2)

                'yesterday's final
                output = "Report Dates: " & date1 & " and " & date2
                filename3 = "filename3.doc"
                SaveToFile(output, filename3)

    'email files here
    SendEmail(to, body,date1);

                'detele temp files
                DeleteFile(filename1)
                DeleteFile(filename2)
                DeleteFile(filename3)
            Catch e As Exception
                cEmail.SendErrorEmail("me@hme.com", e.Message)
            End Try

    End Sub

Sub SaveToFile(ByVal text As String, ByVal fileName As String)
        Dim path As String = "c:\temp\" & fileName
        Try
            If File.Exists(path) = True Then
                File.Delete(path)
            End If

            ' Create a file to write to.
            Dim sw As StreamWriter = File.CreateText(path)
            sw.WriteLine(text)
            sw.Flush()
            sw.Close()

            ' Open the file to read from.
            Dim sr As StreamReader = File.OpenText(path)
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            message = "in SaveToFile  " & e.Message
            cEmail.SendErrorEmail("me@hme.com", message)
        End Try
    End Sub



Sub DeleteFile(ByVal fileName As String)
        Dim path As String = "c:\temp\" & fileName
        Try

            If File.Exists(path) = True Then
                File.Delete(path)
            End If

        Catch e As Exception
            message = "in DeleteFile  " & e.Message
            cEmail.SendErrorEmail("me@hme.com", message)
        End Try
    End Sub

我收到以下错误:

在 DeleteFile 进程无法访问文件 'c:\temp\filename2.doc' 因为它正被另一个进程使用。

我应该在删除文件之前释放任何进程吗?我错过了什么?

编辑:这是我发送文件的“发送电子邮件”功能

 Public Sub SendEmail(ByVal msgTo As String, ByVal msgBody As String, ByVal date1 As String)


        Dim mail As New MailMessage()
        Dim objSMTP As New SmtpClient()
        Dim filename As String

        ''''''''''''''''''''''''''''''''''''''


        Try
            mail.To.Add(msgTo)
            mail.Bcc.Add("me@hme.com")
            mail.Priority = MailPriority.Normal
            mail.IsBodyHtml = True
            mail.Subject = "subject"
            mail.Body = msgBody


            filename = "filename1.doc"
            Dim DOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
            filename = "filename2.doc"
            Dim FOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
            filename = "filename3.doc"
            Dim FOERecords2 As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment


            mail.Attachments.Add(DOERecords) 'add the attachment
            mail.Attachments.Add(FOERecords) 'add the attachment
            mail.Attachments.Add(FOERecords2) 'add the attachment

            objSMTP.Send(mail)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

【问题讨论】:

  • 电子邮件例程可能仍会锁定附件文件。
  • @LarsTech 谢谢!我用我的电子邮件功能更新了代码。那里有什么我想念的吗?
  • 我目前没有设置邮件客户端来测试它。 mail 对象需要在 send 调用之后释放,因此请尝试在 finally 块中释放它。看看这是否有效。否则,您可能只需要输入一个小定时器程序并等待这些文件的释放。

标签: asp.net vb.net


【解决方案1】:

您需要处理所有附件对象,否则您将留下打开的文件句柄。 您可以简单地调用 MailMessage 上的 Dispose。这将触发对任何附件的处置。

objSMTP.Send(mail) 
mail.Dispose()

如果您使用Using 语句封装邮件对象,则可以避免这种情况

Using(mail as MailMessage = New MailMessage())
....
End Using

【讨论】:

    【解决方案2】:

    我会在你使用文件的地方添加一些用法:

            Using sw As StreamWriter = File.CreateText(path)
              sw.WriteLine(text)
              sw.Flush()
              sw.Close()
            End Using
    
              ' Open the file to read from.             
            Using sr As StreamReader = File.OpenText(path)
              Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
              Loop
              sr.Close() 
            End Using
    

    usings 负责由流对象实现的 IDisposable 接口。它将取消分配给文件的锁。

    邮件对象应该实现相同的模式:

            Using mail As New MailMessage()  
              ....
              End Try 
            End Using
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 2011-11-03
      • 2014-02-07
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多