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”的一部分。

这个错误是什么意思?

4

3 回答 3

12
Directory.Delete(path, true);

文档

于 2013-01-02T10:58:29.420 回答
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);
    }
}
于 2014-05-20T05:58:53.313 回答
0

以前的答案可能有效,但我相信它会在 ReadOnly 文件中出现问题。但是为了确保删除和删除任何属性 ReadOnly,执行此过程的最佳方法必须是使用一种方法来方便您执行的方式,您没有使用对象的正确属性,例如,当使用

目录信息.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();
    }
于 2013-01-02T11:18:05.790 回答