【发布时间】:2013-01-04 21:48:57
【问题描述】:
使用 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[] 对象进行调试,它不会抓取从命令提示符中找到的不可见文件。
有什么方法可以删除文件/目录吗?
【问题讨论】:
-
中列出的
.和..条目是元条目。.代表当前目录,..代表父目录。 -
谁会想到一个不熟悉当前和父目录条目的程序员会让我感到非常老了。
标签: c# fileinfo directoryinfo