文件被什么东西阻止了?
File is being blocked by nothing?
我被困在这个例外。我不确定,我的代码的哪一部分可能会阻止 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;
}
}
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);
}
老实说,我找不到,是什么导致了文件块。这应该是所有代码,必须对文件执行某些操作。
其他一切都只是不触及代码。如果您觉得任何方法令人不安,我可以添加任何其他内容。
提前致谢。
EDIT_THE 异常
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.
我认为您没有适当地处理资源。试试这个:
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
接口。我相信你已经找到了困难的原因。 :-)
我被困在这个例外。我不确定,我的代码的哪一部分可能会阻止 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;
}
}
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);
}
老实说,我找不到,是什么导致了文件块。这应该是所有代码,必须对文件执行某些操作。
其他一切都只是不触及代码。如果您觉得任何方法令人不安,我可以添加任何其他内容。
提前致谢。
EDIT_THE 异常
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.
我认为您没有适当地处理资源。试试这个:
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
接口。我相信你已经找到了困难的原因。 :-)