我正在使用Visual Basic,我想计算一个文件夹及其子文件夹中存在的所有文件.
调暗计数器= My.Computer.FileSystem.GetFiles("C:\ Folder")MsgBox(文件数为"& CStr(counter.Count))
,但它仅计数 C:\ Folder 中的文件,而不计数 C:\ Folder \ Sub-Folder \ AnotherSubFolder 中的文件我应该怎么办?谢谢您的帮助!
解决方案使用 Directory.GetFiles()定义如下: msdn.microsoft/zh-cn/library/ms143316(v = vs.110).aspx
所以你只用
Dim计数器为Integer = Directory.GetFiles(someString,"*.*",SearchOption.AllDirectories).Length;MsgBox(文件数为:" +计数器)someString 是您要从其开始的顶级目录
"*.*" 是您要匹配的搜索模式.这将获取所有文件.例如,如果只需要文本文件,则可以执行"*.txt" .
SearchOption 枚举有两个选项: AllDirectories 或 TopDirectoryOnly (如果您仅对确切目录感兴趣)通过了,很明显.
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 :
Dim counter = My.Computer.FileSystem.GetFiles("C:\Folder") MsgBox("number of files is " & CStr(counter.Count))
but it only counts files in the C:\Folder and not in C:\Folder\Sub-Folder\AnotherSubFolder What should I do? Thank's for help!
解决方案Use Directory.GetFiles() as defined here: msdn.microsoft/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)someString being the top-level directory you want to start at
"*.*" being the search pattern you want to match. This gets all files. If you wanted only text files, for example, you could do "*.txt".
SearchOption enum has two options: AllDirectories or TopDirectoryOnly if you're only interested in the exact directory passed, obviously.
更多推荐
计算文件夹和子文件夹中的所有文件
发布评论