75

I've got a junk directory where I toss downloads, one-off projects, email drafts, and other various things that might be useful for a few days but don't need to be saved forever. To stop this directory from taking over my machine, I wrote a program that will delete all files older than a specified number of days and logs some statistics about the number of files deleted and their size just for fun.

I noticed that a few project folders were living way longer than they should, so I started to investigate. In particular, it seemed that folders for projects in which I had used SVN were sticking around. It turns out that the read-only files in the .svn directories are not being deleted. I just did a simple test on a read-only file and discovered that System.IO.File.Delete and System.IO.FileInfo.Delete will not delete a read-only file.

I don't care about protecting files in this particular directory; if something important is in there it's in the wrong place. Is there a .NET class that can delete read-only files, or am I going to have to check for read-only attributes and strip them?

Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
OwenP
  • 24,950
  • 13
  • 65
  • 102
  • 2
    I changed the accepted answer because Gulzar's code sample is more detailed than Tim Stewart's. Should've chose that one in the first place but for some reason I liked Tim's better. People are strange things! – OwenP Apr 30 '09 at 22:13
  • 4
    +1 for taking the time to make things a little more right in the universe. If only my developers would go back and correct our own minor transgressions, we'd be able to cancel the Remedial Programming classes! – Adam Liss May 01 '09 at 11:38

5 Answers5

162

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);
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
  • You get an upvote for being right, but your answer came in after Tim Stewart's which said the same thing. – OwenP Nov 05 '08 at 17:33
  • In some cases you may also need to take ownership of the file before you can clear the readonly flag or delete it. See http://stackoverflow.com/questions/12999272/ for details. – Greg Bray Jan 23 '14 at 21:47
49

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().

Tim Stewart
  • 5,350
  • 2
  • 30
  • 45
22

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

file.IsReadOnly = false;
file.Delete();
Neil
  • 3,899
  • 1
  • 29
  • 25
2

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

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

Hm, I think I'd rather put

>del /F *

into a sheduled task. Maybe wrapped by a batch file for logging statistics.

Am I missing something?

mkoeller
  • 4,469
  • 23
  • 30