Bari
Bari

Reputation: 75

deleting folder and subfolders in c#

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?

Upvotes: 3

Views: 4709

Answers (3)

Ghasem
Ghasem

Reputation: 15573

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);
    }
}

Upvotes: 0

Julio Borges
Julio Borges

Reputation: 662

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();
    }

Upvotes: 0

nk2003dec
nk2003dec

Reputation: 534

Directory.Delete(path, true);

Documentation

Upvotes: 12

Related Questions