为什么我的代码找不到目录

时间:2012-08-12 02:58:30

标签: c# winforms linq directory

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

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

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

2 个答案:

答案 0 :(得分:2)

Var应该是var - 你想要>这在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并尝试一下,这是值得的!

答案 1 :(得分: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);