How do I delete a read-only file?
Asked Answered
M

6

77

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?

Mononucleosis answered 5/11, 2008 at 17:13 Comment(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!Mononucleosis
+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!Freese
C
167

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);
Corella answered 5/11, 2008 at 17:23 Comment(2)
You get an upvote for being right, but your answer came in after Tim Stewart's which said the same thing.Mononucleosis
In some cases you may also need to take ownership of the file before you can clear the readonly flag or delete it. See stackoverflow.com/questions/12999272 for details.Ontologism
A
50

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

Archimandrite answered 5/11, 2008 at 17:19 Comment(1)
It's not about being first.Discourse
R
22

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

file.IsReadOnly = false;
file.Delete();
Reprehensible answered 29/11, 2011 at 12:21 Comment(0)
F
2

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

Freese answered 5/11, 2008 at 17:15 Comment(0)
T
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?

Transpadane answered 5/11, 2008 at 17:18 Comment(2)
Yes. He wants to delete A file when it is of particular age. Not all the files every period.Rotz
He doesn't want to delete all files, just all files older than a certain amount of time.Archimandrite
R
0

To recursively delete a directory which contains read-only files you can do something like this:

public static void DeleteRecursive(DirectoryInfo target)
{
    if (!target.Exists)
    {
        return;
    }

    foreach (var file in target.EnumerateFiles())
    {
        if (file.IsReadOnly)
        {
            file.IsReadOnly = false;
        }

        file.Delete();
    }

    foreach (var dir in target.EnumerateDirectories())
    {
        DeleteRecursive(dir);
    }

    target.Delete();
}
Rugged answered 24/1 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.