2012-05-15 64 views
0
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("[email protected]", 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("[email protected]", 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("[email protected]", message) 
     End Try 
    End Sub 

,我發現了以下錯誤:無法刪除文件

in DeleteFile The process cannot access the file 'c:\temp\filename2.doc' because it is being used by another process.

我應該釋放刪除文件之前的任何進程?我錯過了什麼?

編輯:這裏是發送出文件

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("[email protected]") 
      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 
+1

電子郵件程序可能仍然對附件文件鎖

objSMTP.Send(mail) mail.Dispose() 

這可能是可以避免的。 – LarsTech

+0

@LarsTech謝謝!我用我的電子郵件功能更新了代碼。有沒有我在那裏失蹤的任何事情? –

+1

我目前沒有郵件客戶端設置來測試它。郵件對象需要在發送調用之後處理,因此請嘗試將它放置在finally塊中。看看是否有效。否則,你可能只需要放一點定時器程序並等待這些文件的發佈。 – LarsTech

回答

2

您需要處置的所有附件的對象,或者你會離開打開的文件句柄的後面。 您可以直接在MailMessage上調用Dispose。這將觸發任何附件上的處置。如果封裝郵件對象與Using聲明

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

是的,驗證過。 – LarsTech

+0

mail.dispose()工作。謝謝! –

1

我想補充你身邊使用文件的地方一些usings我的「發送電子郵件」功能:

 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