Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件被另一个进程异常C使用#_C#_.net_Smtp_Mailmessage - Fatal编程技术网

C# 文件被另一个进程异常C使用#

C# 文件被另一个进程异常C使用#,c#,.net,smtp,mailmessage,C#,.net,Smtp,Mailmessage,我已经创建了一个应用程序,可以根据数据库中的信息生成excel文件。这些文件保存在我的硬盘上的一个文件夹中 之后,我附加的文件,并通过邮件发送它们。当我生成另一批文件时,我会删除旧文件,然后创建新文件 我的问题是,当我生成了一批文件并发送它们时,如果我想生成另一批文件,我无法删除其中一个旧文件,因为邮寄方法仍保留着一个excel文件 这是我的密码: public void SendMailedFilesDKLol() { string[] sentFiles=Directory.GetF

我已经创建了一个应用程序,可以根据数据库中的信息生成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文件时,您可能没有关闭文件流,或者在尝试发送电子邮件时,您在excel中打开了文件流


请显示生成excel文件的代码。

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


请显示生成excel文件的代码。

您需要处理附件对象。 使用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());
    }
}

您需要处理附件对象。 使用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());
    }
}

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

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

你确定是你的邮件代码而不是Excel流程保存了你的文档?是的,我是,因为当我注释掉邮件发送方法时,我可以根据自己的意愿重新生成文件你确定是你的邮件代码而不是Excel流程保存了你的文档?是的,我是,因为当我注释掉邮件发送方法时,我可以根据需要重新生成文件