2009-06-25 71 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

相关问题