C# delete read-only directory

Posted on 2007-08-01
Last Modified: 2013-12-17
How do I delete a read-only directory (other than executing attrib)? Is there a way to change the attributes on directories, like there is for files?

Question by:gromul
    LVL 8

    Expert Comment

    FileInfo fi = new FileInfo("");
            fi.Attributes = FileAttributes.Archive;

    you can set attribute and make it deletable
    LVL 21

    Accepted Solution

    I tried the one above and it didn't let me do it... It will work with some correction:

          DirectoryInfo dir = new DirectoryInfo( @"C:\Deleteme\" );
          dir.Attributes = FileAttributes.Normal;
          dir.Delete( true );  // delete recursively

    Moreover, this code will work only if you have directory readonly but all contents normal. Otherwise, you have to iterate through your directory and set all files and folders to Normal (clearing ReadMe) as when you do this:
       dir.Attributes = FileAttributes.Normal;
    it sets it only for your current directory.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now