0

**编辑:有人指出我的代码确实可以工作。但它将“this.zip”文件标识为子文件夹,因此列出了“contents\this.zip”的任何文件夹,这不是我想要的。(试图只找到那些'contents\newfolder\this.zip',所以'contents'目录至少有一个子文件夹)。我怎样才能让它只列出实际文件夹的目录,而不仅仅是 .zip 文件?

我正在编写一个程序来处理标题为 'this.zip' 的文件,这些文件通常直接位于一个名为 'contents' 的文件夹中。我正在尝试确定 this.zip 文件是否直接位于“contents”文件夹中,或者是否有一个子文件夹。我有一组代码正在确定“内容”文件夹是否为空,我认为可以更改它以查找包含子目录的任何“内容”文件夹,但它不起作用。

//This finds 'contents' folders that are empty
            DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
            DirectoryInfo[] folders = directory.GetDirectories("*contents*",SearchOption.AllDirectories);                

            Var query = from folder in folders
                        where folder.GetFileSystemInfos().Length == 0
                        select folder.FullName.ToString();
            foreach (string str in query)
            {
                //this adds the path of any empty 'contents' folder
                listEmptyFolder.Add(str);
            }

我想如果我将查询 - where 语句更改为长度> 1,它将找到任何具有子目录的内容,但它不起作用。这是完成我正在尝试的最佳方法,还是这种尝试使用 LINQ 甚至会起作用?

 //This is supposed to find 'contents' folders that have subdirectories, but it's unsuccessful

            DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
            DirectoryInfo[] folders = directory.GetDirectories("*contents*",SearchOption.AllDirectories);                

            Var query = from folder in folders
                        where folder.GetFileSystemInfos().Length > 1
                        select folder.FullName.ToString();
            foreach (string str in query)
            {
                //this adds the path of any empty 'Contents' folder
                listNestedFolders.Add(str);
            }
4

2 回答 2

2

Var 应该是 var - 你想要 > 0。这在 LinqPad 中运行良好。(我建议测试这些东西。)

DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
DirectoryInfo[] folders = directory.GetDirectories("*contents*",SearchOption.AllDirectories);                

var query = from folder in folders
            where folder.GetFileSystemInfos().Length > 0
            select folder.FullName.ToString();

这是我的 LinqPad 代码,它可以很好地显示所有中间步骤。

void Main()
{
   DirectoryInfo directory = new DirectoryInfo("D:\\test");
   DirectoryInfo[] folders = directory.GetDirectories(@"*.*",SearchOption.AllDirectories);

   var query2 = from folder in folders
                where folder.GetFileSystemInfos().Length > 0
                select folder.FullName.ToString();

  folders.Select(f => f.GetFileSystemInfos()).Dump();
  query2.Dump();

  foreach (string str in query2)
  {
    Console.WriteLine(str);
  }
}

下载 LinqPad 试试看,值得!

于 2012-08-12T03:20:29.647 回答
0

这应该有效:

    public List<string> FindEmptyFolders(string FolderToSearchIn, string FolderNameToSearch, string FileExtension = "")
    {
        List<string> FolderList = System.IO.Directory.GetDirectories(FolderToSearchIn, "*" + FolderNameToSearch + "*", SearchOption.AllDirectories).ToList();
        List<string> EmptyFolders = new List<string>();
        if (string.IsNullOrEmpty(FileExtension))
            FolderList.ForEach(Folder => { if (System.IO.Directory.EnumerateFiles(Folder).Count() == 0) EmptyFolders.Add(Folder); });
        else
            FolderList.ForEach(Folder => { if (System.IO.Directory.EnumerateFiles(Folder, "*." + FileExtension).Count() == 0) EmptyFolders.Add(Folder); });
        return EmptyFolders;
    }

用法:

foreach (string EmptyFolder in FindEmptyFolders(txtbxOldFolder.Text, "contents"))
            MessageBox.Show(EmptyFolder);
于 2012-08-12T04:08:36.267 回答