另一个进程正在使用此文件,因此该进程无法访问此文件。
我做了一个发送邮件的程序,我想在附件发送成功以后,将文件移到Bakup里面,结果提示:另一个进程正在使用此文件,因此该进程无法访问此文件。
请问如何解决?
代码:
bool success;
try
{
success = SendMail(FromMail, mailname, mailpassword, ToMail, smtphost, port, Subject + System.DateTime.Now.ToShortDateString() + fileNames[index].Name, body, fileNames[index].FullName, ccadd, bccadd);
}
catch
{
success = false;
}
if (success)
{
textLog.Text = textLog.Text + "\r\n" + "【" + System.DateTime.Now + "】【成功】" + fileNames[index].FullName;
System.IO.File.AppendAllText(@logName, "【" + System.DateTime.Now + "】【成功】" + fileNames[index].FullName + "\r\n");
if (!Directory.Exists(Attachmentsfile + "\\Bakup"))//判断是否存在
{
Directory.CreateDirectory(Attachmentsfile + "\\Bakup");//创建新路径
}
fileNames[index].Refresh();
System.IO.File.Move(fileNames[index].FullName, Attachmentsfile + "\\Bakup\\" + fileNames[index].Name);
}
else
{
textLog.Text = textLog.Text + "\r\n" + "【" + System.DateTime.Now + "】【失败】" + fileNames[index].FullName;
System.IO.File.AppendAllText(@logName, "【" + System.DateTime.Now + "】【失败】" + fileNames[index].FullName + "\r\n");
}