【问题标题】:File is being blocked by nothing? [duplicate]文件被什么都阻止了? [复制]
【发布时间】:2018-11-08 22:46:54
【问题描述】:

我被这个例外困住了。我不确定,我的代码的哪一部分可能会阻止 xml 文件。

这是 main.cs:

 foreach (var po_no in DatabaseData.getPO_NOs())
            {
                XmlFile.serialize(DatabaseData.read(po_no));
            }

            foreach (var fileToSend in new DirectoryInfo(AppD_Config.getConfigKey("toSendFolder")).GetFiles())
            {
                Mail.Mail.send("xxxx", "xxxx", fileToSend.FullName.Split('\\').Last(), " ", fileToSend.FullName);
                Debug.WriteLine("Sent mail.");
                fileToSend.MoveTo(fileToSend.FullName.Replace(AppD_Config.getConfigKey("toSendFolder"), AppD_Config.getConfigKey("sentFolder")));
//EXCEPTION OCCURS RIGHT UP THIS COMMENT------------------
                }

XmlFile.serialize-

public static string serialize(Order order)
        {
            Directory.CreateDirectory(AppD_Config.getConfigKey("toSendFolder"));
            string fullPath = AppD_Config.getConfigKey("toSendFolder") + order.PO_NO + @"_epce_PO.xml";
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("structure", "ORDERS");
            ns.Add("orders_structure_ver", "1");
            var serializer = new XmlSerializer(order.GetType());
            using (TextWriter writer = new StreamWriter(fullPath))
            {
                serializer.Serialize(writer, order, ns);
                writer.Close();
                return fullPath;
            }
        }

邮件.发送-

public static void send(string from, string to, string subject, string message, string attachmentPath)
        {
            MailMessage mail = new MailMessage(from, to);
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "smtpexch";
            client.Port = 25;

            mail.Attachments.Add(new Attachment(attachmentPath));
            mail.Subject = subject;
            mail.Body = message;
            client.Send(mail);
        }

老实说,我无法找到导致文件阻塞的原因。这应该是所有代码,必须对文件做一些事情。

其他一切都只是不涉及代码。如果您发现任何令人不安的方法,我可以添加任何其他内容。

提前致谢。

EDIT_THE 例外

“System.IO.IOException”类型的未处理异常发生在 mscorlib.dll

附加信息:进程无法访问该文件,因为它 正在被另一个进程使用。

【问题讨论】:

  • @J.vanLangen 我尝试将 Thread.sleep(5000) 放在 mail.send 之后,电子邮件到达我的收件箱,但 5 秒后发生同样的异常。所以我看不出我的 smtp 阻止文件的原因。
  • 你想用Exception 出现的那一行来实现什么?
  • @LittleBit 我需要找出我的代码的哪一部分以某种方式阻止了文件的发送。
  • 发布异常详情如何?您有错误,但我们不知道错误是什么,只是您认为它是什么。
  • 请同时发布异常堆栈,而不仅仅是错误消息。

标签: c# email serialization streamwriter


【解决方案1】:

我相信您没有适当地处理资源。试试这个:

using (SmtpClient client = new SmtpClient())
{
    using (MailMessage mail = new MailMessage(from, to))
    {
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtpexch";
        client.Port = 25;

        using (var attachment = new Attachment(attachmentPath))
        {
            mail.Attachments.Add(attachment);
            mail.Subject = subject;
            mail.Body = message;
            client.Send(mail);
        }                        
    }                    
}

所有使用的类都实现了IDisposable 接口。我相信你已经找到了艰难的原因。 :-)

【讨论】:

  • 好的,这很困难。我将不得不研究哪些类是 Idisposable 的,该死的我不知道附件是什么。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多