-2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Demasterpl
  • 2,103
  • 5
  • 24
  • 32
  • 3
    The `.` and `..` entries listed in are meta entries. `.` represents the current directory and `..` represents the parent directory. – shf301 Jan 04 '13 at 21:50
  • Who would have though it would be a programmer unfamiliar with current & parent directory entries to make me feel terribly old. – Reacher Gilt Jan 04 '13 at 22:08

1 Answers1

3

Try

Directory.Delete(parentFolderPath, true);
Lucas
  • 3,376
  • 6
  • 31
  • 46