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.

major_lost

Recommended Answers

All 3 Replies

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")
    myFiles.Add(file)
Next

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.

commented: +1 +6

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.

ml

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.