1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Option Strict On
Public Class Form1
'ajout d'une listbox
Dim liste As New ListBox With {.Parent = Me, .Width = 400, .Height = 400}
'ajout d'un picturebox
Dim box As New PictureBox With {.Parent = Me, .Bounds = New Rectangle(400, 0, 400, 400), .SizeMode = PictureBoxSizeMode.AutoSize}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'redimmension formulaire
Me.Size = New Size(800, 450)
'ajout événement à l'objet liste
AddHandler liste.SelectedIndexChanged, AddressOf listeclick
'recherche dans tous les disques
For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
If drive.IsReady Then
'ici je recherche les jpg ainsi que les gif
ChercheImages(New IO.DirectoryInfo(drive.RootDirectory.ToString), {"jpg", "gif"})
End If
Next
End Sub
Private Sub listeclick(sender As Object, e As EventArgs)
'affiche l'image sélectionnée dans picturebox
Using reader As New IO.FileStream(liste.Text, IO.FileMode.OpenOrCreate)
box.Image = Image.FromStream(reader)
End Using
End Sub
Private Sub ChercheImages(dossier As IO.DirectoryInfo, Extensions() As String)
Try
'pour chaque extension...
For Each ext As String In Extensions
'...on fait une liste des fichiers concernés...
Dim f() As IO.FileInfo = dossier.GetFiles("*." & ext)
'...et on ajoute les fichiers à la listbox
For Each img As IO.FileInfo In f
liste.Items.Add(img.FullName)
Next
Next
'recherche des sous-dossiers...
For Each d As IO.DirectoryInfo In dossier.GetDirectories
'et recherche récursive des fichiers
ChercheImages(d, Extensions)
Next
Catch ex As Exception
End Try
End Sub
End Class |
Partager