如何搜索所有文件夹和子文件夹中的字符串 [英] How do I search a string in all folder and sub-folder

查看:89
本文介绍了如何搜索所有文件夹和子文件夹中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim path As String = FolderBrowserDialog1.SelectedPath
    Dim di As New DirectoryInfo(path)
    Dim yourSearchString As String
    yourSearchString = "my string"
    For Each Dir As String In Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
        ListBox1.Items.Clear()
        ListBox2.Items.Add(Dir)
    Next
    For Each fi As FileInfo In di.GetFiles()
        If File.ReadAllText(fi.FullName).Contains(yourSearchString) Then
            Console.WriteLine(fi.Name)
            ListBox1.Items.Add(fi.Name)
        End If
    Next
End Sub





我尝试过:



我想在更多文件中搜索一个字符串,我的代码可以在一个文件夹中搜索好,但我希望在文件夹根目录下的子文件夹中进行搜索。!

谁可以帮助我,请!!



What I have tried:

I want search a string in more file, and my code can search in one folder good, but i want this search in sub-folder in folder root.!
Who can help me, Please!!

推荐答案

阅读: https://support.microsoft.com/en-us/kb/306666 [ ^ ]


检查:如何:使用后台线程搜索文件 [ ^ ] - 完整的样本!

另一个指南:如何:通过目录树迭代(C#编程指南) [ ^ ]
Check this: How to: Use a Background Thread to Search for Files[^] - a complete sample!
Another guide: How to: Iterate Through a Directory Tree (C# Programming Guide)[^]


试试这个

Try this
Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories)


这篇关于如何搜索所有文件夹和子文件夹中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆