简体   繁体   English

根据文件夹 C# Winforms 的内容检查和列出子文件夹

[英]Check and list sub-folders based on contents of folders C# Winforms

I have a folder called "C:\\Foo\\" and in that folder, there are folders "Bar" , "Bob" , "Bill" and "BAM" .我有一个名为"C:\\Foo\\"文件夹,在该文件夹中,有文件夹"Bar""Bob""Bill""BAM" In each folder, there are a buncha files.在每个文件夹中,都有一个bunka 文件。

I wanna run through all of "C:\\Foo\\" subfolders and return folder names based on their contents or lack thereof:我想遍历所有"C:\\Foo\\"子文件夹并根据其内容或缺少内容返回文件夹名称:

foreach (folder in folders)
{
    if (folder !contains "someFile.pdf")
    {
        listBox1.Items.Add(folder);
    }

So that listBox1 will fill up with "Bob", "BAM" and "Bill" cuz they don't have someFile.pdf.所以listBox1将填充“Bob”、“BAM”和“Bill”,因为它们没有 someFile.pdf。 So then I can see that Bob, BAM and Bill all don't have someFile.pdf in their folders.然后我可以看到 Bob、BAM 和 Bill 在他们的文件夹中都没有 someFile.pdf。

I've checked around and I've found code that does similar stuff, but nothing to populate an list of subfolders based on their contents.我查了一下,发现代码做了类似的事情,但没有什么可以根据子文件夹的内容填充子文件夹列表。

You will need to use FileSystemObject to iterate through your folders and files.您将需要使用FileSystemObject来遍历您的文件夹和文件。

string[] files = 
    Directory.GetFiles("C:\Foo\","*", SearchOption.AllDirectories);

Refer to MDSN for details: Directory.GetFiles Method详细参考MDSN: Directory.GetFiles方法

Or as @BradleyDotNet suggested (Thanks), use EnumerateFiles :或者按照@BradleyDotNet 的建议(谢谢),使用EnumerateFiles

Refer to Docs for details: Directory.EnumerateFiles有关详细信息,请参阅文档: Directory.EnumerateFiles

string sourceDirectory = @"C:\foo";

    try
    {
        var allFiles
          = Directory.EnumerateFiles(sourceDirectory, "*", SearchOption.AllDirectories);

        foreach (string currentFile in allFiles)
        {
            string fileName = currentFile.Substring(sourceDirectory.Length + 1);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为C#Intellisense生成XML文件,为子文件夹损坏 - Generating XML file for C# Intellisense, broken for sub-folders 忽略特定子文件夹的 C# 编译警告? - Ignore C# compile warnings for specific sub-folders? 从 C# 中的文件夹和子文件夹中获取文件名 - Get file names from folder and sub-folders in C# 如何将文件从文件夹和子文件夹传输到 C# 中由文件名模式创建的特定文件夹(File2specific Folders) - How to transfer files form folders and sub-folders to specific folders created by file name pattern in C# (File2specific Folders) 如何获取azure文件共享存储c#中子文件夹内的所有文件? - How to get all files which is inside sub-folders in azure file share storage c#? C# 在以日期命名的多个子文件夹中获取具有多个通配符的文件 - C# get files with multiple wildcard within multiple sub-folders named after dates 如何计算收件箱子文件夹,包括Outlook中子文件夹下的子文件夹等 - How to count inbox sub-folders including sub-folders under sub-folders and so on in Outlook 复制文件夹但忽略一些子文件夹 - Copy a folder but ignoring some sub-folders 从 Dropbox 上的公共共享文件夹和子文件夹下载图像 - Downloading images from publicly shared folders and sub-folders on Dropbox 使用带有SharpSSH的SFTP下载文件夹和子文件夹 - Download folders and sub-folders using SFTP with SharpSSH
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM