[英]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.