I need a delay in visual basic program

I am writing a visual basic program that display the file from a directory in a list box

the app has a button which calls routines to

1. delete files in the directory

2.put some files in the directory

3. get the file info from directory and add file names to a list box

here is code for step 3

my sub

My.Computer.FileSystem.CurrentDirectory = "C:\mtmp"

' Create a reference to the current directory.

Dim di As New DirectoryInfo(Environment.CurrentDirectory)

' Create an array representing the files in the current directory.

Dim fi As FileInfo() = di.GetFiles()

Dim fiTemp As FileInfo

For Each fiTemp In fi

ListBox1.Items.Add(fiTemp.Name)

Next fiTemp

end sub

the problem occours in step 3. If i set a break point on the first line the code works fine. if I just let in go the fileinfo object is empty thus the list box is empty. How do I work around this



Answer this question

I need a delay in visual basic program

  • Quilnux

    ahmedilyas wrote:

    why dont you use this simple code :-)

    Dim theFiles() as String = System.IO.Directory.GetFiles("Path")

    for each currentItem as String in theFile

    Me.theListBox.Items.Add(currentItem)

    end if

    there is no need to use a fileinfo object - just using extra resources and ineffecient ;-)

    The only reason I could think of to use FileInfo would be to get just the file name, not the path\filename. But I agree simplify it. Here's what I came up with to do that.

    For Each Filename As String In My.Computer.FileSystem.GetFiles("C:\datapath")
    ListBox1.Items.Add(New FileInfo(Filename).Name)
    Next

    It's always interesting to see how many different ways you can do things in the .Net Framework and how the results are often just slightly different depending on what you use. For example, System.IO.Directory.GetFiles returns a string array while My.Computer.FileSystem.GetFiles returns a readonly collection object.



  • pasha2k

    I have tried the various options except the time thing and they dont solve the problem. It appears that the os takes a little time to get the files into the directory so the info routines can read them. Maybet the timer is the way to go.!!!!
  • jwalker343

    If you're waiting for files to arrive you may want to use the FileSystemWatcher component. When it stops firing events it should be time to read the folder's contents. This might work best where you have the total number of incoming files and where you could fire off the load listbox routine when the count is reached.

    You could also use the BackgroundWorker object to sleep the thread where you load the list box for a short moment to wait for the copying to complete.



  • Fabiods

    And there is also

    My.Computer.FileSystem.GetFileInfo(Filename).Name

    I didn't realize that one was there until I started looking at the different ways to do this.

    The System.IO methods return strings, which is slightly faster. The My.Computer.FileSystem methods that return objects seem to add about 10% extra overhead to the operations in my quick stopwatch tests. So it's probably best to favor the System.IO method unless you need the objects for some reason.



  • Nic-Gun

    ahmedilyas wrote:

    you could also do this to just get the filename:

    Dim theFiles() as String = System.IO.Directory.GetFiles("Path")

    for each currentItem as String in theFile

    Me.theListBox.Items.Add(System.IO.Path.GetFileName(currentItem))

    end if

    Have you left an "s" off the end of theFile in the For Each statement

    I'm comparing your code with your line.>>

    Dim theFiles() as String = System.IO.Directory.GetFiles("Path")

    Regards,

    S_DS.



  • Gregg Hamilton

     

    Hi,

    I would put a timer in like this.>>

    Dim myTimer As Timer

    myTimer.Start

    Dim fi As FileInfo() = di.GetFiles()

    Do

    ReDim Preserve fi As FileInfo() = di.GetFiles()

    Loop until myTimer=5000 '5000 is 5000 milliseconds = 5 seconds

    myTimer.Stop

    '  Release the timer resources. 

    myTimer.Close

     

    to give the program the chance to populate the array FileInfo()

    See also.>>

    http://msdn2.microsoft.com/en-us/library/system.timers.timer_methods.aspx

     

     

    Regards,

    S_DS

     



  • robinjam

    what exactly is the problem depending on the number of files and how you are performing the search (such as obtaining subfolders and the depth of that) it will take some time. Generally its quick but again depends on the number of files/folders

  • p.cosmos

    why dont you use this simple code :-)

    Dim theFiles() as String = System.IO.Directory.GetFiles("Path")

    for each currentItem as String in theFiles

       Me.theListBox.Items.Add(currentItem)

    end if

     

    there is no need to use a fileinfo object - just using extra resources and ineffecient ;-)



  • Pauly C

    you could also do this to just get the filename:

    Dim theFiles() as String = System.IO.Directory.GetFiles("Path")

    for each currentItem as String in theFiles

       Me.theListBox.Items.Add(System.IO.Path.GetFileName(currentItem))

    end if



  • Leather

    indeed, thanks for spotting that buddy :-) Much appreciated - ill edit the post

  • I need a delay in visual basic program