c# - 无法删除文件,因为它正被另一个进程使用

标签 c# process using

当我尝试在“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);
            }
        }

最佳答案

您需要将您的 EMail 类修改为:

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

关于c# - 无法删除文件,因为它正被另一个进程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11852938/

相关文章:

c# - 使用 Entity Framework 将 asp.net Identity 实现到现有数据库中

c# - Activator.CreateInstance 找不到构造函数(MissingMethodException)

c# - Windows 8 SDK 是否支持 SQL Server Compact Edition?

c - 诊断进程卡在D状态(不可中断 sleep /阻塞IO)

Python 多处理 - 当最快的进程终止时返回

c++ - 为什么要使用 "using"关键字来访问我的基类方法?

c# - ADO.NET 是否过度使用 IDisposable?

c++ - 这是否使用与此 typedef 相同的函数类型(不是指针)的别名

c# - 如何在 ShowDialog() 阻塞调用之前注册消息处理程序?

c# - 无法重定向进程的标准输出