Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleted file still appears in Directory.GetFiles result

Tags:

c#

I have two webmethods. The first is:

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

The other is:

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

I'm calling these methods like so:

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

Despite deleting the "1.xml" file, the call to Directory.GetFiles("*.xml"); still returns "1.xml" in the results. In other words, it doesn't seem to have been deleted.

And then, when I loop the result , try to read the file , get the FileNoFoundException

like image 278
shenhengbin Avatar asked Jun 23 '11 14:06

shenhengbin


1 Answers

I've found that the DirectoryInfo/FileInfo classes do not always update. In those instances you need to call the Refresh method on the Directory/File instances.

like image 195
JaCraig Avatar answered Sep 19 '22 03:09

JaCraig