MINIMIZAR CODIGO

ALEVALE 06/09/2012 15:12:35
#409269
Pessoal tenho o código abaixo e gostaria de uma forma de minimizar alguém tem alguma sugestão ?

Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFiles.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFilesX64.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFilesX86.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramData.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strWindowsDirectory.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicAll.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicRingtones.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicRingtonesWindows.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicasXPAll.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strTemporaryInternetFile.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strVideosPublic.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.ToString, [Ô]Temporary Internet Files[Ô])) = False Then

Try
For Each fm As String In _FileMasks
Files.AddRange(BaseDirectory.GetFiles(fm))
Next
_
Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try

Try
Dim Directories As New ArrayList

For Each dm As String In _DirectoryMasks
Directories.AddRange(BaseDirectory.GetDirectories(dm))
_Directories.AddRange(Directories)
Next

For Each di As DirectoryInfo In Directories
DoSearch(di)
Next

Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try

End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
KERPLUNK 06/09/2012 18:12:39
#409278
Bem e o que exatamente isso tudo deve fazer?
JABA 06/09/2012 19:28:54
#409280
Tente evitar estruturas aninhadas assim com if, pois dificulta o entendimento humano.
Fiz umas refatorações no seu codigo para termos mais legibilidade.


Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)

if getStatusDiretorios() then

Try
localizarEAdicionarArquivosDoDiretorio()
adicionarDiretorios
Catch

End Try
end if

end sub

private function getStatusDiretorios() as boolean

If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFiles.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFilesX64.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramFilesX86.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strProgramData.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strWindowsDirectory.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicAll.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicRingtones.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicRingtonesWindows.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strMusicasXPAll.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strTemporaryInternetFile.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strVideosPublic.ToString)) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.ToString, [Ô]Temporary Internet Files[Ô])) = False Then
Return true
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
return False
End Function

private sub localizarEAdicionarArquivosDoDiretorio()
Try
For Each fm As String In _FileMasks
Files.AddRange(BaseDirectory.GetFiles(fm))
Next

Catch u As UnauthorizedAccessException
throw u
End Try
end sub

private sub adicionarDiretorios()
Try
Dim Directories As New ArrayList

For Each dm As String In _DirectoryMasks
Directories.AddRange(BaseDirectory.GetDirectories(dm))
_Directories.AddRange(Directories)
Next

For Each di As DirectoryInfo In Directories
DoSearch(di)
Next

Catch u As UnauthorizedAccessException

End Try
end sub
ALEVALE 08/09/2012 11:22:26
#409300
é uma função que busca arquivos pelas extensões nos subdiretorios porém não procura em alguns diretórios, %Arquivos de Programas%,%Temporary Internet Files% etc.

Abaixo meu código com a alteração:

Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.ToString, [Ô]Temporary Internet Files[Ô])) = False Then
Dim val() As String = {strProgramFiles.ToString, strProgramFilesX86.ToString, strProgramFilesX64.ToString, strProgramData.ToString, strWindowsDirectory.ToString, strMusicAll.ToString, strMusicRingtones.ToString, strMusicRingtonesWindows.ToString, strMusicasXPAll.ToString, strTemporaryInternetFile.ToString, strVideosPublic.ToString}
Dim lst As New List(Of String)
lst.AddRange(val)

If lst.Contains(BaseDirectory.FullName.ToString) = False Then
Try
For Each fm As String In _FileMasks
Files.AddRange(BaseDirectory.GetFiles(fm))
Next
_
Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try

Try
Dim Directories As New ArrayList

For Each dm As String In _DirectoryMasks
Directories.AddRange(BaseDirectory.GetDirectories(dm))
_Directories.AddRange(Directories)
Next

For Each di As DirectoryInfo In Directories
DoSearch(di)
Next

Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try
End If

End If

End Sub
Tópico encerrado , respostas não são mais permitidas