2013-01-04 117 views
-2

使用Visual Studio 2010 C#。我试图删除C:/Windows/MyFolderA中的一个文件夹,其中MyFolderA是我的软件放置的文件夹 - 不是微软的。无法删除包含不可见文件的文件夹

我用这个代码删除该文件夹的内容和文件夹本身:

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

如果我试图删除我得到一个异常文件夹

“System.IO .IOException:该目录不是空的“。

在我的GUI上显示不可见文件我没有看到任何文件。用命令提示符查看该文件夹似乎有2个目录:1个已命名。和第二个命名..(不太熟悉命令提示符目录,所以我不知道它们是否是临时名称,或者如果这些是实际的目录名称)在0文件和0字节。

通过查看FileInfo[]对象进行调试,它不会抓取从命令提示符找到的不可见文件。

任何想法如何删除文件/目录?

+3

的'.'和'中列出..'条目元条目。 '.'代表当前目录,'..'代表父目录。 – shf301

+0

谁会有,虽然这将是一个程序员不熟悉当前和父目录条目,让我感到非常古老。 –

回答

3

尝试

Directory.Delete(parentFolderPath, true); 
相关问题