2012-08-07 56 views
3

當我嘗試刪除「foreach」循環中的文件時,最終收到錯誤消息。無法刪除文件,因爲它正在被另一個進程使用

我知道我需要在某處使用關鍵字'using`,但我不確定在哪裏以及如何。

private void btnEmailIntegrationFiles_Click(object sender, EventArgs e) 
{ 
    DialogResult EmailWarningMsg = MessageBox.Show("You're about to email the Integration IAT text files. Are you sure?", "WARNING!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); 

    if (EmailWarningMsg == DialogResult.Yes) 
    { 
     if (Directory.GetFiles(AppVars.NetworkIntegrationFileLocation).Length == 0) 
     { 
      MessageBox.Show("The folder is empty. Please create the files before sending it.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     else 
     { 
      if (Directory.GetFiles(AppVars.NetworkIntegrationFileLocation).Length != 4) 
      { 
       MessageBox.Show("The folder does not contain exactly 4 files.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else 
      { 
       Email email = new Email(); 

       email.SendEmailToFinalDestinationWithAttachments(AppVars.DBTeamEmail, AppVars.ChrisWhitmoreEmail, AppVars.DBTeamEmail, "Integration Files", "Please see integration files attached."); 
      } 
     } 

     PopulateListViewWithPoliciesAvailableToHoldBack(); 

     string[] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation); 

     foreach (string file in files) 
     { 
      File.Delete(file); 
     } 
    } 
} 

我怎麼會去使用關鍵字using爲「發送電子郵件」,這樣我就不會遇到試圖刪除那些被作爲電子郵件附件的文件時,此錯誤消息?

下面是發送電子郵件類:

public void SendEmailToFinalDestinationWithAttachments(string EmailFrom, string EmailTo, string EmailCC, string EmailSubject, string EmailBody) 
     { 
      try 
      { 
       MailMessage EmailMessage = new MailMessage(); 
       SmtpClient smtp = new SmtpClient(AppVars.SMTPClient, AppVars.SMTPClientPort); 
       smtp.UseDefaultCredentials = false; 
       EmailMessage.IsBodyHtml = true; 
       EmailMessage.To.Add(EmailTo); 
       EmailMessage.CC.Add(EmailCC); 
       EmailMessage.CC.Add(user); 
       EmailMessage.Subject = EmailSubject; 
       EmailMessage.From = new MailAddress(EmailFrom); 
       EmailMessage.Body = EmailBody; 

       string[] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation, "*" + DateTime.Now.ToString("yyyyMMdd") + "*"); 

       foreach (string file in files) 
       { 
        EmailMessage.Attachments.Add(new Attachment(file)); 
       } 

       smtp.Send(EmailMessage); 
      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.Message); 
      } 
     } 
+0

大概在電子郵件;但是,你還沒有提供關於這個類的任何細節。這個從哪裏來? SendEmailToFinalDestinationWithAttachments做了什麼等等? – 2012-08-07 19:46:33

+0

清理代碼/重構它,並確保您發佈適合您的問題的所有必要的代碼。 – MethodMan 2012-08-07 19:49:03

+0

提供電子郵件課程。我很確定問題是「string [] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation,」*「+ DateTime.Now.ToString(」yyyyMMdd「)+」*「); foreach(string file in文件) { EmailMessage.Attachments.Add(new Attachment(file)); }「...我怎麼去關於在這裏使用關鍵字」using「? – Testifier 2012-08-07 19:52:17

回答

2

您需要修改您的電子郵件類是這樣的:

using (MailMessage EmailMessage = new MailMessage()) { 
    ... 
    smtp.Send(EmailMessage); 
} 
+0

即時消息會給這個鏡頭,thx! – Testifier 2012-08-07 19:59:51

+0

這工作。謝謝! – Testifier 2012-08-07 20:10:15

相關問題