删除文件时出现文件被另一个进程在用,无法删除?
for (int j = 0; j < index; j++)
            {
                string path = @"D:\mail\MyTest" + j + ".txt";
                File.Delete(path);
            }

解决方案 »

  1.   

    查一下代码,看哪里读了文件流,没有调用close方法关闭
      

  2.   

    已经关闭流了 
    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);
                }
      

  3.   

    你那个sw关闭了,那个sr并没有关闭啊
    StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312")); 
    上面找不到关闭的语句