在C#中删除文件夹和子文件夹

3

我有一个包含子文件夹和只读属性的文件和文件夹的文件夹。我想删除这个文件夹以及其子文件夹和文件。

我编写了以下代码:

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

现在的问题是每次运行程序时都会出现以下错误:

无法找到路径“c:\ex\xx\bb”。

这个错误意味着什么?

1
你为什么要将 DirectoryInfo 转换成字符串?你可以直接使用 DirectoryInfo.GetFiles - Tim Schmelter
这可能意味着您正在尝试删除某个文件或目录,而该目录中没有任何文件。我建议您做几件事情。例如,您应该检查列表是否有 list.Count > 0,还要看看 mm.Delete 是否会抛出错误。此外,您可以搜索如何使用 DirectoryInfo。还有一件重要的事情是,您是否使用调试器逐行执行代码?如果是,请报告哪一行引发了错误。 - MethodMan
请查看以下链接,了解如何设置文件属性:http://www.csharp-examples.net/file-attributes/ - MethodMan
我已经点赞了。请不要给新手负面评价。我只能恢复一次。 - Ken Kin
3个回答

12
Directory.Delete(path, true);

文档


Directory.Delete(path, true) 会报错,正确的写法是 Directory.Delete(path, true); - MethodMan

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

0
之前的回答可能有效,但我认为它在只读文件上会出现问题。但是为了确保删除和移除任何只读属性,最好的方法是使用一种方法来简化您正在进行的方式,您没有使用对象的正确属性,例如,在使用DirectoryInfo.ToString()时,以及使用DirectoryInfo.GetFiles(aa...)时,您没有使用Framework在DirectoryInfo类中提供的资源。请参见以下内容:
    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();
    }

这个程序搜索根目录和第一级子文件夹,我不得不使用System.IO.SearchOption.AllDirectories来包括所有子目录。现在它可以正常工作了。 - Bari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接