另一个进程正在使用此文件,因此该进程无法访问此文件。

harry125 2011-07-23 09:39:23
我做了一个发送邮件的程序,我想在附件发送成功以后,将文件移到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");
}
...全文
827 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ksaponka_tang 2012-08-04
  • 打赏
  • 举报
回复
sendmail.msg.Dispose();
言多必失 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 harry125 的回复:]
引用 3 楼 server126 的回复:
if (!Directory.Exists(DatabaseBackUpPath))//不存在备份指定文件目录,则创建
Directory.CreateDirectory(DatabaseBackUpPath);
if (!File.Exists(LogPath + "\\" + LogFileName))///创建日志文件
File.Creat……
[/Quote]

写日志会影响移动的,写完要关闭的啊,不会关闭移动就会提示啊。 这和“我要是的是葫芦”那篇课文很像啊
harry125 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 server126 的回复:]
if (!Directory.Exists(DatabaseBackUpPath))//不存在备份指定文件目录,则创建
Directory.CreateDirectory(DatabaseBackUpPath);
if (!File.Exists(LogPath + "\\" + LogFileName))///创建日志文件
File.Create(LogPath + "\\" ……
[/Quote]

不是写日志文件的问题,是移动文件出错。
我那个写日志文件是正常的,也很简洁。
PSSonyXbox 2011-07-23
  • 打赏
  • 举报
回复
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();
}
PSSonyXbox 2011-07-23
  • 打赏
  • 举报
回复
.Close();
harry125 2011-07-23
  • 打赏
  • 举报
回复
就是这行代码出的问题:
System.IO.File.Move(fileNames[index].FullName, Attachmentsfile + "\\Bakup\\" + fileNames[index].Name);
harry125 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qq81867376 的回复:]
引用 4 楼 harry125 的回复:
引用 3 楼 server126 的回复:
if (!Directory.Exists(DatabaseBackUpPath))//不存在备份指定文件目录,则创建
Directory.CreateDirectory(DatabaseBackUpPath);
if (!File.Exists(LogPath + "\\" + LogFileName)……
[/Quote]

我把写日志去掉了,还是提示同样的错误。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧