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.
10 posts Page 1 of 1
Contributors
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

H3LP
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?
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: H3LP
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
http://vagex.com/?ref=25000
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: H3LP
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
Last edited by mandai on Mon Dec 27, 2010 2:33 pm, edited 3 times in total.
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

Re: H3LP
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 :)
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

Re: H3LP
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 :(
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: H3LP
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.
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

Re: H3LP
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
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: H3LP
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
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

Re: H3LP
upperdrag
addItem is not defined :(
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: H3LP
mandai
It is defined in the previous post.
10 posts Page 1 of 1
Return to “Tutorial Requests”