0

I am stuck at this exception. I am not sure, which part of my code could be blocking the xml file.

This is 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;
            }
        }

Mail.Send-

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

I am honestle not able to find, what is causing the file block. This should be all the code, that has to do something with the file.

Everything else is just not touching the code. I can add anything else if you would find any method disturbing.

Thanks in advance.

EDIT_THE EXCEPTION

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file because it is being used by another process.

Tomáš Filip
  • 727
  • 3
  • 6
  • 23

1 Answers1

1

I believe you are not disposing of resources appropriately. Try this:

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

All the classes being used implement the IDisposable interface. I believe you have find out why the hard way. :-)

JuanR
  • 7,405
  • 1
  • 19
  • 30
  • Ok, that was the hard one. I will have to do reasearch which classes are Idisposable, damn I didnt know the attachment was. Thanks. – Tomáš Filip Nov 08 '18 at 14:56