vb.net 计算文件夹和子文件夹中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32277848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting all files in folder and subfolder
提问by Technologuy
I am using visual basic and I want to count all the files that exist in a folder and in its subfolders.. I tried this :
我正在使用visual basic,我想计算一个文件夹及其子文件夹中存在的所有文件。我试过这个:
Dim counter = My.Computer.FileSystem.GetFiles("C:\Folder")
MsgBox("number of files is " & CStr(counter.Count))
Dim counter = My.Computer.FileSystem.GetFiles("C:\Folder")
MsgBox("number of files is " & CStr(counter.Count))
but it only counts files in the C:\Folderand notin C:\Folder\Sub-Folder\AnotherSubFolderWhat should I do? Thank's for help!
但它只能算作中的文件C:\文件夹,并没有在C:\文件夹\子文件夹\ AnotherSubFolder我应该怎么办?感谢帮助!
回答by sab669
Use Directory.GetFiles()as defined here: https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx
Directory.GetFiles()按此处定义使用:https: //msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx
So you'd just use
所以你只需要使用
Dim counter As Integer = Directory.GetFiles(someString, "*.*", SearchOption.AllDirectories).Length;
MsgBox("Number of files is : " + counter)
someStringbeing the top-level directory you want to start at
someString作为您想要开始的顶级目录
"*.*"being the search pattern you want to match. This gets all files. If you wanted only text files, for example, you could do "*.txt".
"*.*"是您要匹配的搜索模式。这将获取所有文件。例如,如果您只需要文本文件,则可以执行"*.txt".
SearchOptionenum has two options: AllDirectoriesor TopDirectoryOnlyif you're onlyinterested in the exact directory passed, obviously.
SearchOptionenum 有两个选项:AllDirectories或者TopDirectoryOnly如果您只对传递的确切目录感兴趣,显然。

