已删除的文件仍显示在Directory.GetFiles结果中

本文关键字:Directory GetFiles 结果 显示 删除 文件 | 更新日期: 2023-09-27 18:00:25

我有两个web方法。第一种是:

void deleteFile(string filePath)
{
  File.Delete(filePath);
}

另一种是:

string[] getAllFile()
{
  // at the same folder....
  Directory.GetFiles("*.xml");
  .....
  return ....   
}

我这样称呼这些方法:

deleteFile("1.xml")
getAllFile();

尽管删除了"1.xml"文件,但对Directory.GetFiles("*.xml");的调用仍然会在结果中返回"1.xml"。换句话说,它似乎没有被删除。

然后,当我循环结果时,尝试读取文件,获得FileNoFoundException

已删除的文件仍显示在Directory.GetFiles结果中

我发现DirectoryInfo/FileInfo类并不总是更新的。在这些实例中,您需要对目录/文件实例调用Refresh方法。

我不知道,但我怀疑您的web方法的结果被缓存在某个地方。