日期:2014-05-18  浏览次数:20986 次

另一个进程正在使用此文件,因此该进程无法访问此文件。
我做了一个发送邮件的程序,我想在附件发送成功以后,将文件移到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");
  }

------解决方案--------------------
.Close();
------解决方案--------------------
if (!Directory.Exists(DatabaseBackUpPath))//不存在备份指定文件目录,则创建
Directory.CreateDirectory(DatabaseBackUpPath);
if (!File.Exists(LogPath + "\\" + LogFileName))///创建日志文件
File.Create(LogPath + "\\" + LogFileName).Close();
using (StreamWriter write = File.AppendText(LogPath + "\\" + LogFileName))
{
write.WriteLine(DateTime.Now.ToString() + "----备份情况:【失败】1次");
write.WriteLine("错误信息: " + ex.Message);
write.Flush();
write.Close();
}
------解决方案--------------------
探讨
引用:
if (!Directory.Exists(DatabaseBackUpPath))//不存在备份指定文件目录,则创建
Directory.CreateDirectory(DatabaseBackUpPath);
if (!File.Exists(LogPath + "\\" + LogFileName))///创建日志文件
File.Creat……