计算文件夹和子文件夹中的所有文件

编程入门 行业动态 更新时间:2024-10-13 12:21:50
本文介绍了计算文件夹和子文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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.

更多推荐

计算文件夹和子文件夹中的所有文件

本文发布于:2023-05-28 00:43:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308157.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!