runtime: FileInfo.Delete() does not update .Exists

FileInfo.Delete() does not update .Exists

So I just noticed:

	// this file exists
	var fi = new FileInfo(@"S:\Ome\File.path");
	// here fi.Exists == true
	fi.Delete();
	// still fi.Exists == true
	fi.Refresh();
	// finally fi.Exists == false

The code I wrote assumed Delete() changes .Exists and it was failing very unexpectedly. Checked the actual source code and found Delete() is implemented as:

	public override void Delete() => FileSystem.DeleteFile(FullPath);

instead of:

	public override void Delete()
	{
	    FileSystem.DeleteFile(FullPath);
	    Invalidate();
	}

Why is this simple operation not updating .Exists if it succeeds? On the other hand MoveTo() changes the fi object completely updating to the new path. So if you do a Delete() you need to followup with a Refresh() while a MoveTo() is good (even if it would have been better if Move() returned a new FileInfo with the new details instead of changing the current one).

Is this inconsistency by design or was it just overlooked?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

I agree that updating the behavior consistently when users explicitly change state is the way to go. We need to document this breaking change, of course.

@JeremyKuhne you can assign me to this if it’s still available

Thank you yes @carlossanlop this it’s fixed I think