Today I will teach you how to display files from your computer in a listbox. The code is very simple and is for beginners so lets get started! First open visual basic 2008 / 2010.
1. Create / open a project.
2. Add a Button and give it the text: "Search".
3. Also add a Textbox and a Listbox.
4. Now double click on the Button and type or copy the following code:
First type above the code "Try". Press enter and the following code will appear:
Now we want to search the code for the whole computer. For that, we need to replace:
The last thing I want to tell you is that if the program didn't find a file, it shows a message.
Now place the following code between the Catch ex As Exception and End Try:
1. Create / open a project.
2. Add a Button and give it the text: "Search".
3. Also add a Textbox and a Listbox.
4. Now double click on the Button and type or copy the following code:
For Each File In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Desktop, FileIO.SearchOption.SearchAllSubDirectories, Nothing) Dim foundFile As String = My.Computer.FileSystem.GetFileInfo(File).Name If foundFile.Contains(TextBox1.Text) Then ListBox1.Items.Add(foundFile) End If NextWith this code you can only search on your desktop. So if you don't have the file on your desktop, your program will freeze. So we are going to solve that.
First type above the code "Try". Press enter and the following code will appear:
Try Catch ex As Exception End TryNow place the code we wrote between Try and Catch ex As Exception.
Now we want to search the code for the whole computer. For that, we need to replace:
My.Computer.FileSystem.SpecialDirectories.DesktopWith:
"C:\"And know it searcher for files on your whole computer.
The last thing I want to tell you is that if the program didn't find a file, it shows a message.
Now place the following code between the Catch ex As Exception and End Try:
MsgBox("The file you want to search doesn't exist.")The finished code will look like this :
Try For Each File In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Desktop, FileIO.SearchOption.SearchAllSubDirectories, Nothing) Dim foundFile As String = My.Computer.FileSystem.GetFileInfo(File).Name If foundFile.Contains(TextBox1.Text) Then ListBox1.Items.Add(foundFile) End If Next Catch ex As Exception MsgBox("The file you want to search doesn't exist.") End Try
1 comments:
you are the best
ReplyPost a Comment
Note: Only a member of this blog may post a comment.