Page 1 of 1

H3LP

Posted: Mon Dec 27, 2010 12:23 am
by upperdrag
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?

Re: H3LP

Posted: Mon Dec 27, 2010 1:49 am
by Axel
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

Re: H3LP

Posted: Mon Dec 27, 2010 2:13 am
by mandai
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

Re: H3LP

Posted: Mon Dec 27, 2010 2:23 am
by upperdrag
mandai wrote:
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

This is the reason i love mandai :)

Re: H3LP

Posted: Mon Dec 27, 2010 2:30 am
by upperdrag
mandai wrote:
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
lol it became unresponsive :(

Re: H3LP

Posted: Mon Dec 27, 2010 1:40 pm
by mandai
You can use a backgroundworker while it is searching:
Code: Select all
    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
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.
You can also use IO.File.ReadAllText to get the file contents.

Re: H3LP

Posted: Tue Dec 28, 2010 1:59 am
by upperdrag
Hey! Mandai That really helps, Thanks :D I'm Gonna Test It Out!

Edit* - Dude sorry but it does not works for me, i planted a test file in my documents and the program became unresponsive.. lol

Re: H3LP

Posted: Tue Dec 28, 2010 2:49 pm
by mandai
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:
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

Re: H3LP

Posted: Wed Dec 29, 2010 5:10 am
by upperdrag
addItem is not defined :(

Re: H3LP

Posted: Wed Dec 29, 2010 5:38 pm
by mandai
It is defined in the previous post.