删除文件时出现文件被另一个进程在用,无法删除?
for (int j = 0; j < index; j++)
{
string path = @"D:\mail\MyTest" + j + ".txt";
File.Delete(path);
}
for (int j = 0; j < index; j++)
{
string path = @"D:\mail\MyTest" + j + ".txt";
File.Delete(path);
}
using (StreamWriter sw = new StreamWriter(dir, false, System.Text.Encoding.GetEncoding("GB2312")))
{
for (int i = 0; i < index; i++)
{
string path = @"D:\mail\MyTest" + i + ".txt";
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
string line = "";
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("date:"))
{
date = line.Trim().Replace("date", "发件日期") + "|";
sb.Append(date);
}
if (line.StartsWith("from:"))
{
from = line.Trim().Replace("from", "发件人邮箱") + "|";
sb.Append(from);
}
if (line.StartsWith("to:"))
{
to = line.Trim().Replace("to", "收件人邮箱") + "|";
sb.Append(to);
}
if (line.StartsWith("subject:"))
{
subject = line.Trim().Replace("subject:", "邮件名称:未送达") + "|";
sb.Append(subject);
}
}
strRep[0] = sb.ToString().Split('|')[2].ToString() + "|";//发件日期
strRep[1] = sb.ToString().Split('|')[0].ToString() + "|";//发件人邮箱
strRep[2] = sb.ToString().Split('|')[1].ToString() + "|";//收件人邮箱
strRep[3] = sb.ToString().Split('|')[3].ToString() + "|";//邮件名称
strRep[4] = "错误信息:" + strBody[0]; sbNew.Append(strRep[0]);
sbNew.Append(strRep[1]);
sbNew.Append(strRep[2]);
sbNew.Append(strRep[3]);
sbNew.Append(strRep[4]);
sw.Write(sbNew.ToString());
sb.Remove(0, sb.Length);
sbNew.Remove(0, sbNew.Length);
sw.WriteLine();
}
}
sw.Flush();
sw.Close();
sw.Dispose();
}
for (int j = 0; j < index; j++)
{
string path = @"D:\mail\MyTest" + j + ".txt";
File.Delete(path);
}
StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
上面找不到关闭的语句