【问题标题】:cannot delete file because it's being used by another process无法删除文件,因为它正被另一个进程使用
【发布时间】:2012-08-04 21:06:32
【问题描述】:

当我尝试在“foreach”循环中删除文件时,最后收到错误消息。

我知道我需要在某处使用关键字“使用”,但我不确定在哪里以及如何使用。

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);
            }
        }

【问题讨论】:

  • 可能在电子邮件上;但是,您没有提供有关此类的任何详细信息。这个从哪里来? SendEmailToFinalDestinationWithAttachments 有什么作用?
  • 清理代码/重构它,并确保发布所有符合您问题的必要代码。
  • 提供电子邮件类。我很确定问题出在 "string[] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation, "" + DateTime.Now.ToString("yyyyMMdd") + ""); foreach (文件中的字符串文件){ EmailMessage.Attachments.Add(new Attachment(file)); }" ...我将如何在此处使用关键字“使用”?
  • 这不是你的问题的原因,但你到底为什么要不断重做目录读取?最后一个其实很危险——如果中间有其他东西被扔进去,它会被盲目杀死。
  • 你是对的。我应该只删除应该在那里的文件......尽管该文件夹中不应该有其他文件......

标签: c# process using


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 2016-09-21
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多