Note that there are some explanatory texts on larger screens.

plurals
  1. POcannot delete file because it's being used by another process
    primarykey
    data
    text
    <p>I'm getting the error message at the end, when I try to delete files in the "foreach" loop.</p> <p>I know I need to be using the keyword ´using` somewhere but I'm not sure where and how.</p> <pre><code>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); } } } </code></pre> <p>How would I go about using the keyword <code>using</code> for "emailing" so that I don't run into this error message when trying to delete those files that were emailed as attachments?</p> <p>Here's the send email class:</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload