Rev Jim:
I need to get a random file name from a specific directory (folder) each time this routine is called. I was given these steps on another website, but I need to be shown how this is done. Can you please assist?

Get the files - Directory.GetFiles or DirectoryInfo.GetFiles
Get the file count - Array.Length
Generate the random number - Random.Next
Get the random file - index the array using the random number

I have no clue how to do this. Please help.


2 Years
Discussion Span
Last Post by Reverend Jim

It's actually less trouble to just show you the code than it is to chastize you for not showing any effort to solve it yourself ^_^.

Dim myFiles As New List(Of String)

For Each file As String In My.Computer.FileSystem.GetFiles("D:\temp")

Dim rnd As New Random
MsgBox(myFiles(rnd.Next(0, myFiles.Count)))

What do you know. I was able to do both. I don't know of any vb.net book that doesn't cover these beginner level techniques.

Edited by Reverend Jim

Votes + Comments

Please don't "chastize" me. I am just learning VB 2010 express (I got it for FREE), and it didn't come with a book.
My initial VB experience came from VB 4.0 which I also got FREE with books, but as you well know, VB has changed alot over the years. Thank you for the assist. I specifically ask for you because I knew you of all people could do this. I'll try harder on my own next time, before I ask for any help. Thank you again.


Edited by major_lost


As long as I can see that you are putting in effort to learn I don't have a problem helping out. If you google vb.net tutorials online you can find lots of free training such as this one.

And it was more of a gentle reminder (I did include a smiley) than a chastizing. I've seen more than my share of people who just post their homework assignments and don't even bother to explicitly ask for help. Certainly you do not fall into that category.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.