H3LP
Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
10 posts
Page 1 of 1
upperdrag wrote:Hey Guys Its Me Again, i need 1 help
how do you search for the file throughout the whole computer? i mean the specified file. And then read the .xml file?
Code: Select all
Imports System.IO
Dim Dir as Directoryinfo = new Directoryinfo("Directory")
dim fo() as FileInfo = Dir.Getfiles("*.xml")
For each Fi as FileInfo in fo
Listbox1.items.Add(fi.Name)
Next
P.S. I invented this while reading so tell me if you get errors :P
Try:
Code: Select all
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim drive As String = Path.GetPathRoot(Environment.SystemDirectory)
Dim files As String() = IO.Directory.GetFiles(drive, "*.xml", SearchOption.AllDirectories)
'^ consider using a different thread if the UI becomes unresponsive.
For i As Integer = 0 To files.Length - 1
ListBox1.Items.Add(files(i))
Next
End Sub
Last edited by mandai on Mon Dec 27, 2010 2:33 pm, edited 3 times in total.
mandai wrote:Try:Code: Select allPrivate Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim drive As String = Path.GetPathRoot(Environment.SystemDirectory) Dim files As String() = IO.Directory.GetFiles(drive, "*.xml", SearchOption.AllDirectories) '^ consider using a different thread if the UI becomes unresponsive. For i As Integer = 0 To files.Length - 1 ListBox1.Items.Add(files(i)) Next End Sub
This is the reason i love mandai

mandai wrote:Try:lol it became unresponsive :(Code: Select allPrivate Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim drive As String = Path.GetPathRoot(Environment.SystemDirectory) Dim files As String() = IO.Directory.GetFiles(drive, "*.xml", SearchOption.AllDirectories) '^ consider using a different thread if the UI becomes unresponsive. For i As Integer = 0 To files.Length - 1 ListBox1.Items.Add(files(i)) Next End Sub
You can use a backgroundworker while it is searching:
You can also use IO.File.ReadAllText to get the file contents.
Code: Select all
If you want the files to be shown as they are found instead of all at the end you will need to search in each sub folder with a recursive function. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Delegate Sub addItem(ByVal item As String)
Sub del_addItem(ByVal item As String)
ListBox1.Items.Add(item)
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim drive As String = Path.GetPathRoot(Environment.SystemDirectory)
Dim files As String() = IO.Directory.GetFiles(drive, "*.xml", SearchOption.AllDirectories)
Dim d As [Delegate] = New addItem(AddressOf del_addItem)
For i As Integer = 0 To files.Length - 1
ListBox1.Invoke(d, files(i))
Next
End Sub
You can also use IO.File.ReadAllText to get the file contents.
It should be responsive, though it will fail if it comes across a directory that it cannot read due to security settings.
Here is a recursive function:
Here is a recursive function:
Code: Select all
Sub searchFolder(ByVal folder As String)
Dim files As String()
Try
files = Directory.GetFiles(folder, "*.xml")
Catch ex As Exception
GoTo listFolders
End Try
For i As Integer = 0 To files.Length - 1
ListBox1.Invoke(New addItem(AddressOf del_addItem), files(i))
Next
listFolders:
Dim folders As String()
Try
folders = Directory.GetDirectories(folder)
Catch ex As Exception
Return
End Try
For i As Integer = 0 To folders.Length - 1
searchFolder(folders(i))
Next
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim drive As String = Path.GetPathRoot(Environment.SystemDirectory)
searchFolder(drive)
End Sub
10 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023