Question Random file starter

brekke

Member
Joined
Oct 17, 2010
Messages
11
Location
Stavanger, Norway
Programming Experience
Beginner
Hi guys and ladies!

I have searched up and down for someone who can help me with this little problem!
I need to get a button to enter the applications subfolder and randomly select 1 file to be played in the windows media player COM which i have put in the form.
I have tried creating a shuffling playlist but i cant get it to stop after one file is played (it just goes on shuffeling to another file, and i really do not want a playlist). I manage to create a batch file to be started to randomly pick a file in that folder, but when i do this the file is being played in the systems default player..

Any one got any ideas on how i can solve this obstacle?

Many thanks in advance!
 
As always, divide and conquer.

First, you want something random. That basically always means using the Random class. You create an instance and call the Next method to generate a random Integer in a specified range. You then use that Integer as appropriate for the situation. If you might need more than one random number then you assign the Random object to a member variable and use the same object multiple times, rather than creating a new object each time, which can cause problems.

In your case, what will be the appropriate way to use the random number? You want to choose one song at random from a list, so you need to create a list of songs and then use the random number as an index into that list.

Your songs are files in a specific folder, so how will you create a list? The Directory.GetFiles or DirectoryInfo.GetFiles method will give you an array containing either the file names or FileInfo objects representing the files.

So, putting the pieces together: call GetFiles to create a file list, call Next on a Random object using the length of the list as the maximum, use the random number as an index to get a random file, then play the file.
 
Thanks.. since i am just a newbie and do not actually do this as an occupation it was a bit unclear to me. But i'm a logic thinker so i'll try to figure it out. I did got a hint of how it could be put together.. so many thanks!
btw it's not songs, it's .avi extension ;)

I'll come back if i'm really stuck..
 
Back
Top