Click here to Skip to main content
15,920,468 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.....

VB
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?
Posted
Updated 18-Oct-10 21:22pm
v4
Comments
Dalek Dave 19-Oct-10 3:18am    
Minor Edit for Grammar.
Tarun.K.S 19-Oct-10 10:47am    
setting or elevating program access wont be of any help except if you go deep into some Windows System folders!

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! :)
 
Share this answer
 
v2
Comments
Marc A. Brown 19-Oct-10 9:46am    
EDIT: Fixed code block.
Tarun.K.S 19-Oct-10 9:48am    
Thanx Marc!
Dale 2012 19-Oct-10 16:32pm    
Just because you have been the only one to provide an actual code I will give you 5 out of 5 but will you also be able to help me with the FileIOPermission Class. I need to be able to give permission to the restricted files I am trying to search through and seeing as you have already looked at the code will you be still willing to help me out?
Dale 2012 19-Oct-10 16:42pm    
I know your code is probably self explanatory but just so that I dont confuse anything will you please tell me how to call this module into the scan or does it work automaticly? I have not much expierence with modules...... I thank you so much thoe for providing this :) :)
Tarun.K.S 20-Oct-10 4:50am    
Thanx Dale! well to see how it works, just create a new module in your project.Copy the code above into it, and run it! the result will be shown in a command prompt window coz i am displaying it using console.writeline().
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!! :)
 
Share this answer
 
v2
Comments
Dale 2012 26-Oct-10 22:45pm    
I will be very well willing to show my entire source code if you are able to help with a few questions.. please let me know as soon as you can

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900