2009-06-25 60 views
2

我有一些代碼,我用C#寫的問題。刪除文件失敗,因爲和現有的過程

我使用MailMessage和SMTP組件發送文檔。我將我希望發送到臨時目錄(如c:\ temp)的文件複製到文件,並將它們附加到電子郵件中。

電子郵件發送正常,但是當我嘗試從臨時目錄中刪除的文件,我得到以下錯誤:

The process can not access the file because it is being used by another process

我不明白爲什麼會這樣。下面是處理

public void sendDocument(String email, string barcode, int requestid) 
    { 

     string tempDir = @"c:\temp"; 

     //first we get the document information from the database. 
     Database db = new Database(dbServer, dbName, dbUser, dbPwd); 

     List<Document> documents = db.getDocumentByID(barcode); 
     int count = 0; 
     foreach (Document doc in documents) 
     { 
      string tempPath = tempDir + "\\" + doc.getBarcode() + ".pdf"; 
      string sourcePath = doc.getMachineName() + "\\" + doc.getFilePath() + "\\" + doc.getFileName(); 

      //we now copy the file from the source location to the new target location 
      try 
      { 
       //this copies the file to the folder 
       File.Copy(sourcePath, tempPath, false); 

      } 
      catch (IOException ioe) 
      { 
       count++; 

       //the file has failed to copy so we add a number to the file to make it unique and try 
       //to copy it again. 
       tempPath = tempDir + "\\" + doc.getBarcode() + "-" + count + ".pdf"; 
       File.Copy(sourcePath, tempPath, false); 

      } 

      //we now need to update the filename in the to match the new location 
      doc.setFileName(doc.getBarcode() + ".pdf");     

     } 

     //we now email the document to the user. 
     this.sendEmail(documents, email, null); 

     updateSentDocuments(documents, email); 

     //now we update the request table/ 
     db.updateRequestTable(requestid); 


     //now we clean up the documents from the temp folder. 
     foreach (Document doc in documents) 
     { 
      string path = @"c:\temp\" + doc.getFileName(); 
      File.Delete(path); 
     } 

    } 

我的認爲this.sendEmail()方法將中發送的電子郵件返回sendDocument方法之前文件的代碼,因爲我認爲這是導致SMTP對象刪除失敗。

這是sendEmail方法:

public void sendEmail(List<Document> documents, String email, string division) 
    { 
     String SMTPServer = null; 
     String SMTPUser = null; 
     String SMTPPwd = null; 
     String sender = ""; 
     String emailMessage = ""; 

     //first we get all the app setting used to send the email to the users 
     Database db = new Database(dbServer, dbName, dbUser, dbPwd); 

     SMTPServer = db.getAppSetting("smtp_server"); 
     SMTPUser = db.getAppSetting("smtp_user"); 
     SMTPPwd = db.getAppSetting("smtp_password"); 
     sender = db.getAppSetting("sender"); 
     emailMessage = db.getAppSetting("bulkmail_message"); 

     DateTime date = DateTime.Now; 

     MailMessage emailMsg = new MailMessage(); 

     emailMsg.To.Add(email); 

     if (division == null) 
     { 
      emailMsg.Subject = "Document(s) Request - " + date.ToString("dd-MM-yyyy"); 
     } 
     else 
     { 
      emailMsg.Subject = division + " Document Request - " + date.ToString("dd-MM-yyyy"); 
     } 

     emailMsg.From = new MailAddress(sender); 
     emailMsg.Body = emailMessage; 

     bool hasAttachements = false; 

     foreach (Document doc in documents) 
     { 

      String filepath = @"c:\temp\" + doc.getFileName(); 

      Attachment data = new Attachment(filepath); 
      emailMsg.Attachments.Add(data); 

      hasAttachements = true; 


     } 

     SmtpClient smtp = new SmtpClient(SMTPServer); 

     //we try and send the email and throw an exception if it all goes tits. 
     try 
     { 
      if (hasAttachements) 
      { 
       smtp.Send(emailMsg); 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("EmailFailure"); 
     } 


    } 

如何我解決這個問題,佔用我要刪除的文件的過程。

一旦應用程序結束,我可以刪除文件。

回答

6

你的電子郵件沒有被處理完畢,儘量處置它您發送之後:

try 
{ 
     if (hasAttachements) 
     { 
      smtp.Send(emailMsg);   
     } 
} 
catch ... 
finally 
{ 
     emailMsg.Dispose(); 
} 
1

第一步是找出哪些進程持有問題的文件。我會抓住SysInternals工具包並使用handle.exe命令來確定哪些進程正在保存到文件中。

不知道什麼進程打開文件,沒有辦法解決這個問題。

的Sysinternals鏈接:http://technet.microsoft.com/en-us/sysinternals/default.aspx

+0

感謝鏈接到這些工具...非常有用... – 2009-06-25 16:02:18

0

這裏我只是discoverred一招,相信能對其他人有用嗎? 在向消息添加附件之前,在使用包裝器中創建附件。這確保了它被正確處置,允許文件被成功刪除。我不確定發送是否也需要在這個循環中; (當我測試的時候,電子郵件一開始沒有通過,然後半小時後我被淹沒了,所以決定在網絡稍微平靜的時候離開測試)。

using (Attachment attachment = new Attachment(filename)) 
{ 
    message.Attachments.Add(attachment); 
    client.SendAsync(message, string.Empty); 
} 
File.Delete(filename); 

對我的作品OK反正:)

祝你好運,

JB

相關問題