How do I delete a read-only file?

Solution 1:

According to File.Delete's documentation,, you'll have to strip the read-only attribute. You can set the file's attributes using File.SetAttributes().

using System.IO;

File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);

Solution 2:

According to File.Delete's documentation,, you'll have to strip the read-only attribute. You can set the file's attributes using File.SetAttributes().

Solution 3:

The equivalent if you happen to be working with a FileInfo object is:

file.IsReadOnly = false;
file.Delete();

Solution 4:

Why do you need to check? Just forcibly clear the read-only flag and delete the file.