无法从平面文件中删除记录c#

时间:2015-07-10 08:04:09

标签: c# c#-4.0 flat-file

我正在尝试使用c#删除文本文件中的特定行:

 public static void DeleteProject(int id)
    {
        var fileloc = WebConfigurationManager.AppSettings["FFProject-Manager"];

        var fileloc2 = WebConfigurationManager.AppSettings["TempFile"];

        string line = null;

        using (StreamReader reader = new StreamReader(fileloc))
        {
            using (StreamWriter writer = new StreamWriter(fileloc2))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    string uidCompare = Convert.ToString(line.Split(',')[0]);
                    string uidToCompare = Convert.ToString(id);
                    if (String.Compare(uidCompare, uidToCompare) == 0)
                        continue;

                    writer.WriteLine(line);
                }

            }

        }

        File.Delete(fileloc);
        File.Move(fileloc2, fileloc);
    }

}

但是当我尝试执行它时会出现错误:

The process cannot access the file 'C:\Flat_Files\Project-Manager.txt' because it is being used by another process.

我哪里错了?

0 个答案:

没有答案