我以为通过关闭和部署我的阅读器来解决这个问题,但仍然在某些情况下该文件正在使用中。接下来我打电话给垃圾收集器,这个文件将被释放。 这解决了所有会导致此错误的问题的99%。 代码使用:该进程无法访问该文件,因为它正在被另一个进程删除时使用
public override void Finish()
{
// Kill the reader!
if (_reader != null)
{
_reader.Close();
_reader.Dispose();
// Make sure the server doesn't hold the file
GC.Collect();
}
DeleteFile();
}
完成是一个很大的过程,将处理文件的内容后调用。
当我处理文件只有1(或很少)行我有时得到此错误。它似乎是快速和DeleteFile();
失败的窗口。 我很难再现这个错误,但有时它只是连续发生两次。
当我处理文件需要多于2秒的时间来处理时,这绝不会发生。
我无法使用,因为文件可以是GB的,并且当内存变得太满时,Windows不喜欢它。此外,这种方式的过程表现更好。
问题:
有什么我可以做,以防止这个错误?注:请随时索取更多信息。
编辑:
代码删除文件
protected void DeleteFile()
{
// Delete the file
if (FileName != null && File.Exists(FileName))
File.Delete(FileName);
}
代码来创建文件
protected void WriteFile()
{
// Prepare variables
string path = Path.GetTempPath();
path += "\\MyTempFile";
// Modifiy path to avoid overwriting an existing file.
path += ".csv";
// Write the file to the temp folder
using (FileStream fs = new FileStream(path, FileMode.Create))
{
fs.Write(MyFile, 0, MyFile.Length);
}
// Was the writing successfully completed?
_FileName = File.Exists(path) ? path : null;
}
代码来创建读者
protected override void ReadFile()
{
if (FileName == null)
WriteFile();
// Read the lines
_reader = new StreamReader(FileName, Encoding.Default, true);
while (_reader.Peek() != -1)
{
TotalRows++;
_reader.ReadLine();
}
// Read the lines
_reader = new StreamReader(FileName, Encoding.Default, true);
}
我使用抽象类确定如何读取输入。 通过以下语句,我将遍历文件的内容。
while (FileReader.NextRow(out currentRow, out currentRowNumber))
{
// Process current Row...
}
方法NextRow()看起来这
public override bool NextRow(out List<object> nextRow, out int rowNumber)
{
if (RowNumber > TotalRows)
{
nextRow = null;
rowNumber = 0;
return false;
}
// Set the row number to return
rowNumber = RowNumber;
// Prepare the row
nextRow = _reader.ReadLine().ExtensionThatProcessesTheRow();
RowNumber++;
return true;
}
while循环关闭后我所说的精细加工工艺。 FileReader.Finish();
您不需要调用GC.Collect。你不应该这样做,并且它不应该影响删除文件的能力。 – Joe
访问文件的代码是什么样的?如果我们能看到它,我们可以在那里提出更好的建议。 – Josh
我们需要查看您用于创建,与*进行交互以及*删除这些文件的所有代码。你有可能在某处泄漏了一个对象,或者两个线程正在同时与同一个文件进行交互。 –