Introduction
Sometimes we have very good and long playlists, but the problem appears when we want to copy the songs from it to any destination like your mobile or your mp3. It is really hard to catch every song from a different folder or partition.
You can download the full code (*.cs file) and the program from the link above.
Note: The program can deal with non-English file names.
As you see in the picture, the opened playlist contains an Arabic file name.
Background
I think no background is needed as the idea is very simple.
We will open the playlist file (which is a normal text file), then build an array with the locations and build a checklist in the form with songs' names with a copy button.
The Code Explained
First we have a public
array. So we can use it in all program controls (to be accessed by open dialog and save dialog).
public string[] Final_Adresses=new string[0];
After that, we need to treat the playlist language if it is a non-English language.
byte[] all_bytes = File.ReadAllBytes(openFileDialog1.FileName);
byte[] new_encoded = Encoding.Convert(Encoding.Default, Encoding.Unicode, all_bytes);
string uniString = Encoding.Unicode.GetString(new_encoded);
string[] final_encoded = uniString.Split('\n');
In the code above, we read all playlist files as a byte array, then we convert it to Unicode(utf-8). Then we convert the Unicode byte array to a string and we split the new string to get the original names with Unicode characters so it will NOT be displayed like (??????) or any undefined characters.
Then we will make an array with the address, but the problem with the m3u playlists is that it depends on the song location from the playlist itself.
For example:
If the song is in the same directory, the playlist will contain the song name only.
If the song is in a subdirectory in the same directory of the playlist, it will contain the “directory name\song name“, or else, it will contain the full address.
So we will make an array to contain all addresses in unified format.
System.Collections.ArrayList address = new System.Collections.ArrayList();
for (int x = 2; x < final_encoded.Length; x = x + 2)
{
if (final_encoded[x].Contains(":"))
{
address.Add(final_encoded[x].Remove
(final_encoded[x].Length - 1));
checkedListBox1.Items.Add(final_encoded[x].Remove(0,
final_encoded[x].LastIndexOf('\\') + 1));
}
else
{
string tmpadrs = openFileDialog1.FileName;
tmpadrs = tmpadrs.Remove(tmpadrs.LastIndexOf('\\'));
tmpadrs = tmpadrs + "\\" + final_encoded[x];
address.Add(tmpadrs);
checkedListBox1.Items.Add(final_encoded[x].Remove(0,
final_encoded[x].LastIndexOf('\\') + 1));
}
}
Final_Adresses = (string[])address.ToArray(typeof(string));
Above, we make some checks to determine if the song is in the same directory of the playlist or in another place.
Save or Copy Button
We check if the user checked any songs or NOT, then we copy them.
if (checkedListBox1.CheckedItems.Count > 0)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
ProgressBar1.Maximum = checkedListBox1.CheckedItems.Count;
for (int i = 0; i < Final_Adresses.Length; i++)
{
if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
File.Copy(Final_Adresses[i],
folderBrowserDialog1.SelectedPath +
Final_Adresses[i].Remove(0,
Final_Adresses[i].LastIndexOf('\\')));
ProgressBar1.PerformStep();
}
}
MessageBox.Show("Copying Selected Items Completed");
}
}
else
{
MessageBox.Show("No Items Selected To Copy");
}
Points of Interest
You can deal with any playlist with the same way. Other formats are easier to get the song path as they store the full path with NO conditions for its location.
I may make another copier in the future. If you are interested in this, please visit my blog www.free-codes.blogspot.com.
History
- 25th May, 2009: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.