3

I have a folder that contains sub folders and files with read only attribute (both files and folders). I want to delete this folder with sub-folders and files.

I wrote this code:

static void Main(string[] args)
{        
    DirectoryInfo mm = new DirectoryInfo(@"c:\ex");
    string aa = Convert.ToString(mm);
    string[] allFileNames = 
        System.IO.Directory.GetFiles(aa, 
                                     "*.*", 
                                     System.IO.SearchOption.AllDirectories);
    string[] alldirNames = 
       System.IO.Directory.GetDirectories(aa, 
                                        "*", 
                                        System.IO.SearchOption.AllDirectories);

    foreach (string filename in allFileNames)
    {
        FileAttributes attr = File.GetAttributes(filename);
        File.SetAttributes(filename, attr & ~FileAttributes.ReadOnly);

    }

    foreach (string dirname in alldirNames)
    {
        FileAttributes attr = File.GetAttributes(dirname);
        File.SetAttributes(dirname, attr & ~FileAttributes.ReadOnly);
        Directory.Delete(dirname  , true);
    }

    FileInfo[] list = mm.GetFiles();

    foreach (FileInfo k in list)
    {
        k.Delete();
    }
    mm.Delete();
    Console.ReadKey();
}

The problem now is that whenever I run the program it gives me the following error:

Could not find a part of the path 'c:\ex\xx\bb'.

What does this error mean?

4
  • 1
    Why are you converting the DirectoryInfo to a string? You can use DirectoryInfo.GetFiles directly. Commented Jan 2, 2013 at 10:52
  • it probably means that you trying to delete something or some file where that file directory has zero files.. there are a few things Iwould suggest doing.. for example.. list you should check to see if the list.Count > 0 also is it throwing an error at mm.Delete..? also google how to use DirectoryInfo one more important thing have you used the Debugger to step thru the code..? and if so please report on which line threw the error.. Commented Jan 2, 2013 at 10:53
  • look at this link as well on how to Set FileAttributes, csharp-examples.net/file-attributes Commented Jan 2, 2013 at 10:58
  • I voted up. Please not to giving negative votes to a newbie. I can only recover this for once. Commented Jan 2, 2013 at 11:05

3 Answers 3

12
Directory.Delete(path, true);

Documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Direcory.Delete(path, true) will error it's Directory.Delete(path, true);
0

The previous answer might work, but I believe it will occur with problems in ReadOnly files. But to ensure the deletion and removal of any attribute ReadOnly, the best way to perform this procedure you must be using a method to facilitate the way you were doing, you were not using the correct properties of objects, for example, when using

DirectoryInfo.ToString ()

and use the

DirectoryInfo.GetFiles (aa ...

you were not using the resources the Framework offers within the DirectoryInfo class. See below:

    void DirectoryDelete(string strOriginalPath)
    {
        DirectoryInfo diOriginalPath = new DirectoryInfo(strOriginalPath);
        if (diOriginalPath.Attributes.HasFlag(FileAttributes.ReadOnly))
            diOriginalPath.Attributes &= ~FileAttributes.ReadOnly;

        string[] lstFileList = Directory.GetFiles(strOriginalPath);
        string[] lstdirectoryList = Directory.GetDirectories(strOriginalPath);

        if (lstdirectoryList.Length > 0)
        {
            // foreach on the subdirs to the call method recursively
            foreach (string strSubDir in lstdirectoryList)
                DirectoryDelete(strSubDir);
        }

        if (lstFileList.Length > 0)
        {
            // foreach in FileList to be delete files
            foreach (FileInfo fiFileInDir in lstFileList.Select(strArquivo => new FileInfo(strArquivo)))
            {
                // removes the ReadOnly attribute
                if (fiFileInDir.IsReadOnly)
                    fiFileInDir.Attributes &= ~FileAttributes.ReadOnly;

                // Deleting file
                fiFileInDir.Delete();
            }
        }

        diOriginalPath.Delete();
    }

1 Comment

this searches the root directory and the first level of subfolder i had to use System.IO.SearchOption.AllDirectories to include all subdirectories it works now
0
EmptyFolder(new DirectoryInfo(@"C:\your Path"))
Directory.Delete(@"C:\your Path");

private void EmptyFolder(DirectoryInfo directoryInfo)
{
    foreach (FileInfo file in directoryInfo.GetFiles())
    {       
       file.Delete();
     }

    foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
    {
      EmptyFolder(subfolder);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.