获取文件方法仍然不搜索子文件夹,请帮助!! [英] Get File Method Still does not search subfolders PLEASE HELP!!

查看:97
本文介绍了获取文件方法仍然不搜索子文件夹,请帮助!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经在程序中进行了更改,但是代码仍然不允许或进入根目录的子文件夹.如果我在组合框中选择C:\,它仍然只会进入一个级别,而不会更深入.
有什么想法吗?

开始按钮获取文件方法.....

Ok I have made the change in my program but the code still does not allow or go into the subfolders in the root directory. If I select C:\ in the combobox it still only goes one level and no deeper.
Any ideas?

Start button get file method.....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
        Dim fileList As New List(Of String)(searchForTheseFiles)
        Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
        Try
            Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
            For Each foundFile As FileInfo In files
                If fileList.Contains(foundFile.Name.ToUpper) Then
                    ListBox3.Text &= Environment.NewLine & foundFile.FullName
                    My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
                End If
            Next
        Catch ex As UnauthorizedAccessException
            Debug.WriteLine(String.Format("Could not access directory '{0}'.", myDir.FullName))
        End Try


:(请修复我!!

我有响应告诉我设置或提升对计算机上文件系统的程序访问权限,但如何完全完成?


:( fix me please!!

I have had responces that tell me to set or elevate program access to the file system on my computer but how is done fully?

推荐答案

实际上,您必须递归地调用您的功能..

这是我的代码:

Actually you have to recursively call your function..

Here is my code :

Module BrowseFoldersEx
    Sub main()
        Dim direc As New DirectoryInfo("C:\abhishek")
        If direc.Exists = False Then
            MsgBox("The path does not exist. Please specify a correct path")
            Exit Sub
        End If
        Console.WriteLine("Folders are : ")
        RepeatDir(direc)
        Console.ReadLine()
    End Sub
    Function RepeatDir(ByVal drr As DirectoryInfo)
        Dim fls As DirectoryInfo
        For Each fls In drr.GetDirectories()
            Console.WriteLine(fls.Name)
            RepeatDir(fls)
            Console.WriteLine("Checking for Files:")
            RepeatFiles(fls)
        Next
        
    End Function
    Function RepeatFiles(ByVal dirs As DirectoryInfo)
        Dim fl As FileInfo
        For Each fl In dirs.GetFiles()
            Console.WriteLine(fl.Name)
        Next
        If fl Is Nothing Then
            Console.WriteLine("No files are present in this folder " & dirs.Name)
        End If
    End Function
End Module


希望这可以帮助! :)


Hope this helps! :)


现在这是您搜索文件等的按钮事件.

< pre>私有子Button1_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button1.Click

昏暗searchForTheseFiles()为字符串= TextBox1.Text.ToUpper.Split(Environment.NewLine)
昏暗的fileList作为新列表(字符串)(searchForTheseFiles)
将myDir设为新的DirectoryInfo(CheckedComboBoxEdit1.Text)
试试
昏暗的files()为FileInfo = myDir.GetFiles("*.*",SearchOption.AllDirectories)
对于每个foundFile作为FileInfo在文件中
如果fileList.Contains(foundFile.Name.ToUpper)然后
ListBox3.Text& = Environment.NewLine& foundFile.FullName
My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4,AudioPlayMode.Background)
如果结束
下一个
捕获为UnauthorizedAccessException
Debug.WriteLine(String.Format(无法访问目录"{0}".",myDir.FullName))
结束尝试
结束Sub</pre>

所以现在递归搜索子目录和文件..i将使用两个函数.

< pre lang ="vb">
''声明此列表
将aList变暗为新列表(字符串)

此功能将递归搜索目录

公共函数递归(ByVal _direc作为DirectoryInfo)作为列表(字符串)
Dim dr As DirectoryInfo
对于_direc.GetDirectories()中的每个博士 totalFolders = totalFolders + 1
递归(dr)
RecurFile(dr)
下一个
返回aList
最终功能

在这里,我将实现您的功能:

公共函数RecurFile(ByVal adir作为DirectoryInfo)

对于adir.GetFiles()中每个FoundFile作为FileInfo的文件
如果fileList.Contains(foundFile.Name.ToUpper)然后
ListBox3.Text& = Environment.NewLine& foundFile.FullName
''此处列表将被填充
''alist.add(foundFile.FullName)My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4,AudioPlayMode.Background)
如果结束
下一个

最终功能

因此,您的新button_click应该是:

私有子Button1_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button1.Click

昏暗searchForTheseFiles()为字符串= TextBox1.Text.ToUpper.Split(Environment.NewLine)
昏暗的fileList作为新列表(字符串)(searchForTheseFiles)
将myDir设为新的DirectoryInfo(CheckedComboBoxEdit1.Text)

''此列表将包含文件名
''显示此列表中的文件!
将myList变暗为List(of String)=递归(myDir)

结束子

</pre>

这应该工作戴尔!如果不这样做,则尝试向我展示整个项目以及代码,以更深入地了解您想做什么!祝你好运!! :)
now this is your button event which searches files etc.

<pre>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
Try
Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
For Each foundFile As FileInfo In files
If fileList.Contains(foundFile.Name.ToUpper) Then
ListBox3.Text &amp;= Environment.NewLine &amp; foundFile.FullName
My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
End If
Next
Catch ex As UnauthorizedAccessException
Debug.WriteLine(String.Format("Could not access directory ''{0}''.", myDir.FullName))
End Try
End Sub</pre>

So now to recursively search through sub directories and files..i will be using two functions.

<pre lang="vb">
''declare this list
Dim aList As New List(Of String)

This function will recursively search through the directories

Public Function Recurse(ByVal _direc As DirectoryInfo) As List(Of String)
Dim dr As DirectoryInfo
For Each dr In _direc.GetDirectories()
totalFolders = totalFolders + 1
Recurse(dr)
RecurFile(dr)
Next
Return aList
End Function

Here i will implement your function :

Public Function RecurFile(ByVal adir As DirectoryInfo)

For Each foundFile As FileInfo In adir.GetFiles()
If fileList.Contains(foundFile.Name.ToUpper) Then
ListBox3.Text &amp;= Environment.NewLine &amp; foundFile.FullName
'' here the list will be populated
'' alist.add(foundFile.FullName) My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
End If
Next

End Function

So your new button_click should be :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)

''This List will contain the file names
''Display the files from this list!
dim myList as List(of String)= Recurse(myDir)

End Sub

</pre>

This should work Dale! if it doesn''t then try to show the whole project to me together with the code for a deeper understanding of what you want to do! Good Luck!! :)


这篇关于获取文件方法仍然不搜索子文件夹,请帮助!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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