Home > Net >  Be urgent! Vb.net how to traverse the folder and subfolders and files, each of the recursion?
Be urgent! Vb.net how to traverse the folder and subfolders and files, each of the recursion?

Time:11-01

Recursion and folders can now

But

Now this code will use recursive recursive after all this folder, put into a set of
Then use the fileinfo. Filefull callback all path

Is it at the time of recursion, my program cannot compare file information

May 16th is I want to

But it continued to recursive
Until after all recursive callback

Is there a way to make it through a callback is a?

If the error code I will send to me, I now is a phone, everybody to help solve, I only had 20 points, thank you! Urgent!!!!

CodePudding user response:

With the yield return return IEnumerable

CodePudding user response:

reference 1st floor stherix response:
with yield return return IEnumerable

 
Public Sub GetAllFile (ByVal path As String)
Dim strDir As String ()=System. IO. Directory. GetDirectories (path)
Dim strFile As String ()=System. IO. Directory. GetFiles (path)
Dim As Integer I
If strFile. Length & gt; 0 Then
For I=0 To strFile. Length - 1
The Debug. Print (strFile (I))
Next
End the If
If strDir. Length & gt; 0 Then
For I=0 To strDir. Length - 1
GetAllFile (strDir (I))
Next
End the If
End Sub




The no, 1. The full path to the output file one by one all not a line
2. Or slow, I have no search all files of heaven

I am this, but slowly
 
Public Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount () As" Long
Dim timeBegin As Long=GetTickCount ()
Dim dir As New DirectoryInfo (" D: \ ")
Dim finfo () As the FileInfo=dir. GetFiles (" * ", SearchOption. AllDirectories)
For Each fChild As the FileInfo finfo In
Console. WriteLine (fChild. FullName) output
'Next

CodePudding user response:

Directory. EnumerateDirectories
Directory. EnumerateFiles
With these

CodePudding user response:

C # of this do you seen the


The
reference 3 floor stherix response:
Directory. EnumerateDirectories
Directory. EnumerateFiles
Use the
you that this is too slow, is there a synchronized method, each traversal is a file, it returns a full path to the file name

CodePudding user response:

Thank you, my own c # transformation of a function, to share! One by one, step by step file
Public files
Public files2

Public Sub AddDirFiles (ByVal dir As String)
On the Error Resume Next
Dim di As DirectoryInfo=New DirectoryInfo (dir)
Dim fis As the FileInfo ()=di. GetFiles (" * ")
For Each fi As the FileInfo fis In
Files.=fi FullName
If files=files2 Then
The Else
Console. WriteLine (files)
End the If
Files2=files
Next
Dim dis As DirectoryInfo ()=di. GetDirectories ()
For Each item As DirectoryInfo In dis
AddDirFiles (item. FullName)
Next
End Sub

CodePudding user response:

Can be compared directly in the statement, in addition to add stop recursion condition,

Below is I write a simple example, for your reference:

Dim bStop=false 'set a stop global variable recursive

Public Sub GetAllFile (ByVal path As String, ByVal strFindInfo As String)
Dim strDir As String ()=System. IO. Directory. GetDirectories (path)
Dim strFile As String ()=System. IO. Directory. GetFiles (path)
Dim As Integer I
Dim strTemp as string

If bStop=true, then the exit sub

If strFile. Length & gt; 0 Then
For I=0 To strFile. Length - 1
StrTemp=strFile (I)
If IO. File. GetCreationTime (strTemp)=strFindInfo then 'here by file generated time comparison, can according to the actual need to modify the
BStop=true
Debug. Print (strTemp) 'output only need the content of the
The exit sub
End the if
Next
End the If
If strDir. Length & gt; 0 Then
For I=0 To strDir. Length - 1
GetAllFile (strDir (I))
Next
End the If
End Sub

Recursive faster to use multiple processes, this I can't write: - (

CodePudding user response:

Know more about # 1 # 4 floor "iterator" design mode, you don't have a code in the iterator pattern programming,

CodePudding user response:

Is vb.net code you wrote, I wrote vb.net slightly a little "feel not accustomed to" (actually the.net only experience different under the different language, no big difference), so they do not, for example,

The iterator pattern, in order to apply to the iteration syntax, for example the foreach... The in... Syntax that can be enumerated data sources for "delay query, iteration (note!) , can at any time in advance (note! Interrupt the query,

CodePudding user response:

Alas, groping for you write a case of vb.net, although said. The.net language ability, but never used temporary change after all kinds of language, to write this type of code, or need to spend a few minutes of habit,
 Imports System. IO 

The Module Module1

Sub Main ()
Dim CNT As Integer=0
Dim result=Search (New DirectoryInfo (" c: \ "), "*. *") _
The Where (Function (file)
Return the file. The Name. The StartsWith (" ab ")
End Function) _
Take (20)
For Each file In result
CNT +=1
Console. WriteLine (" {0}, {1} ", CNT, the file. The FullName)
Next
Console. WriteLine (" press any key to end ")
The Console. ReadKey ()
End Sub

The Iterator Function Search (dir As DirectoryInfo, the pattern As String) As IEnumerable (Of the FileInfo)
For Each file In dir. EnumerateFiles (pattern)
Yield the file
Next
For Each subDir In dir. EnumerateDirectories ()
For Each file In Search (subDir, pattern)
Yield the file
Next
Next

End the Function


End the Module

CodePudding user response:

The
reference 4 floor MIAIONE reply:
c # that you have seen it


Quote: refer to the third floor stherix response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related