Question

Using Visual Studio 2010 C#. I'm attempting to delete a folder in C:/Windows/MyFolderA where MyFolderA is a folder placed there by my software - Not Microsoft's.

I've used this code to delete the contents of the folder and the folder itself:

foreach (FileInfo tempfi in listOfMSIInstallers)
{
    //Delete all Files
    DirectoryInfo localDirectoryInfo = new DirectoryInfo(targetDirectory);
    FileInfo[] listOfMSIInstallers = localDirectoryInfo.GetFiles("*",SearchOption.AllDirectories);
    File.SetAttributes(tempfi.FullName, File.GetAttributes(tempfi.FullName) & ~FileAttributes.ReadOnly); //Remove Read-Only
    File.Delete(tempfi.FullName); //Delete File

    string parentFolderPath = "C:/Windows/MyFolderA"; //Example string for StackOverflow

    //Remove ReadOnly attribute and delete folder
    var di = new DirectoryInfo(parentFolderPath);
    di.Attributes &= ~FileAttributes.ReadOnly;
    Directory.Delete(parentFolderPath);
}

If I attempt to delete the folder I get an exception

"System.IO.IOException: The directory is not empty".

Showing invisible files on my GUI I do not see any. Looking at the folder with a command prompt there appears to be 2 directories: 1 named . and the second named .. (not too familiar with command prompt dir so I don't know if they're temp names or if those are the actual directory names) both at 0 files and 0 bytes.

Debugging through looking at the FileInfo[] object, it doesn't grab the invisible files found from command prompt.

Any ideas how I can delete the files/directory?

Was it helpful?

Solution

Try

Directory.Delete(parentFolderPath, true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top