Skip to content

FileInfo.Delete() does not update .Exists #31425

@CodeAngry

Description

@CodeAngry

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?

Metadata

Metadata

Assignees

Labels

area-System.IOenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions