【问题标题】:File is used by another process exception C#文件被另一个进程异常C#使用
【发布时间】:2013-03-10 16:03:18
【问题描述】:

我创建了一个应用程序,它可以根据数据库中的信息生成 excel 文件。这些文件保存在我的硬盘上的一个文件夹中。

然后我附上文件并通过邮件发送它们。当我生成另一批文件时,我删除旧文件,然后创建新文件。

我的问题是,当我生成了一批文件然后发送它们,我想生成另一批文件时,我无法删除其中一个旧文件,因为邮寄方式仍在保留其中一个 excel 文件.

这是我的代码:

public void SendMailedFilesDKLol() {
    string[] sentFiles=Directory.GetFiles(some_Folder);

    if(sentFiles.Count()>0) {
        System.Net.Mail.SmtpClient client=new System.Net.Mail.SmtpClient("ares");
        System.Net.Mail.MailMessage msg=new System.Net.Mail.MailMessage();

        msg.From=new MailAddress("system@lol.dk");
        msg.To.Add(new MailAddress("lmy@lol.dk"));
        msg.Subject="IBM PUDO";

        msg.Body=
            sentFiles.Count()+" attached file(s) has been sent to the customer(s) in question ";

        msg.IsBodyHtml=true;

        foreach(string file in sentFiles) {
            Attachment attachment=new Attachment(file);
            msg.Attachments.Add(attachment);
        }

        client.Send(msg);
    }
}

我曾尝试处理客户端元素,但没有帮助。

谁能帮我解决这个问题?

【问题讨论】:

  • 您确定是您的邮件代码而不是 Excel 进程保留了您的文档?
  • 是的,我是,因为当我注释掉邮件发送方法时,我可以根据需要重新生成文件

标签: c# .net smtp mailmessage


【解决方案1】:

System.Net.Mail.MailMessage 和 System.Net.Mail.SmtpClient 都是 IDisposable 类。您可以尝试以下方法,

    using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("ares"))
    {
       using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
       {
          msg.From = new MailAddress("system@lol.dk");
          msg.To.Add(new MailAddress("lmy@lol.dk"));
          msg.Subject = "IBM PUDO";
          msg.Body = sentFiles.Count() + " attached file(s) has been sent to the customer(s) in question ";
          msg.IsBodyHtml = true;
          foreach (string file in sentFiles)
          {
              Attachment attachment = new Attachment(file);
              msg.Attachments.Add(attachment);
          }

          client.Send(msg);
        }
     }

【讨论】:

    【解决方案2】:

    听起来您在生成 excel 文件时可能没有关闭文件流,或者在尝试通过电子邮件发送它们时在 excel 中打开了它们。

    请您出示您生成excel文件的代码。

    【讨论】:

      【解决方案3】:

      您需要处置附件对象。 使用 LINQ 的示例:

      public void SendMailedFilesDKLol() {
          string[] sentFiles=Directory.GetFiles(some_Folder);
      
          if(sentFiles.Count()>0) {
              System.Net.Mail.SmtpClient client=new System.Net.Mail.SmtpClient("ares");
              System.Net.Mail.MailMessage msg=new System.Net.Mail.MailMessage();
      
              msg.From=new MailAddress("system@lol.dk");
              msg.To.Add(new MailAddress("lmy@lol.dk"));
              msg.Subject="IBM PUDO";
      
              msg.Body=
                  sentFiles.Count()+" attached file(s) has been sent to the customer(s) in question ";
      
              msg.IsBodyHtml=true;
      
              var attachments = sentFiles.Select(f => new Attachment(f)).ToList();
      
              attachments.ForEach(a => msg.Attachments.Add(a));
      
              client.Send(msg);
      
              attachments.ForEach(a => a.Dispose());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多