1

I am making a program which browses files, puts them into an array, and then the user puts those files in a random order into a new array called playlist.

To be clear I do not want to assign one array to the other array. I only want to assign one of the many elements of one array to another existing array.

How is this done?

Here is all I can think of:

playlist[1] = files[5];
2
  • wtf why downvote? If both arrays are strings, and files[5] = "C:\\music\\whydownvote.mp3" the expected result is that playlist[1] = 'C:\\music\\whydownvote.mp3" Commented Sep 3, 2015 at 1:42
  • What exactly 'this' you want to know? How to show the user a form that allows him to browse/select files? How to get filenames of files that match a given pattern (ending with .mp3, for instance) in a given directory? Commented Sep 3, 2015 at 1:57

2 Answers 2

3

You can utilize the System and System.IO namespace for all that fairly easily.

var files = Directory.GetFiles("directory path");
var playlist = new string[files.Length];

At that point, you'll have two arrays. The files array will contain the full path for every file in the directory you specified, and playlist will be a string array with the same size as the files array.

To get a random file and assign it to the playlist array, you can use the Random class in the System namespace to get a random number between a range.

var random = new Random();
int index = random.Next(0, playlist.Length);

You can use a bunch of logical statements to make sure that you don't copy over one file more than once, and that the space you're copying it to isn't already taken up by a file. But you had the idea idea. Transferring all the paths would look something like this

playlist[RandomPlaylistIndex] = files[RandomFileIndex];

with the lefthand side being the receiving end. That's pretty much the gist of it, anyhow. I can post more code if you're still stuck.

Sign up to request clarification or add additional context in comments.

9 Comments

Im new to C#, what is a var?
It's an implicit type. The variable's type is defined by whatever it's first given. Mind you that this is very different from the Object data type. You can read more about it on this page on MSDN. msdn.microsoft.com/en-us/library/bb383973.aspx
I'm still stuck, I don't understand anything (I tried not to say "anything"). What I am trying to accomplish is to choose a random files[n] string and add it to the playlist array playlist[0], then random another files string and add it again to playlist[1], and so on(but not adding say the 6th files string more than once).
If you're awfully new to C#, I'd recommend looking up some video tutorials or other beginner tutorial material to help get you familiar with the basics of the language. Have you tried using the code that I showed you? If so, could you show me what you wrote?
I don't understand what playlist[RandomPlaylistIndex] = files[RandomFileIndex]; is
|
3

Use Array.Copy where you can define the Start and End of the source Array.

For instance, to copy the 15th element of Files to PLaylist:

  Array.Copy(Files, 15, 1, Playlist, Playlist.Length,1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.