An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsFormsApplication1.exe
private void btnCreate_Click(object sender, EventArgs e)
{
OpenFileDialog newPlaylist = new OpenFileDialog();
newPlaylist.InitialDirectory = "C:\\Users\\mklsingh\\Documents\\Visual Studio 2013\\Projects\\Media Player\\WindowsFormsApplication1\\Media Files";
newPlaylist.Filter = "MP3 Audio File (*.mp3)|*.mp3| Windows Media File (*.wma)|*.wma|WAV Audio File (*.wav)|*.wav|All Files (*.*)|*.*";
newPlaylist.RestoreDirectory = false;
newPlaylist.Multiselect = true; // Enables the user to choose multiple files.
if (newPlaylist.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = newPlaylist.SafeFileNames; // Sets the variable 'files' to get the actual filename of the media file, which is the title of the file a user sees.
paths = newPlaylist.FileNames; // Sets the variable 'paths' to get the actual location/path where the file is located/placed.
for (int list = 0; list < files.Length; list++) // A for-statement that will check how many files where chosen.
{
lbPlaylist.Items.Add(files[list]); // Adds the files to the list box as a listbox item.
}
}
}
The code below saves the playlist as XML file:
private void btnSave_Click(object sender, EventArgs e)
{
StreamWriter Write;
SaveFileDialog savePlaylist = new SaveFileDialog();
savePlaylist.RestoreDirectory = false;
try
{
savePlaylist.InitialDirectory = "C:\\Users\\mklsingh\\Documents\\Visual Studio 2013\\Projects\\Media Player\\WindowsFormsApplication1\\Media Files\\Playlist";
savePlaylist.Filter = ("XML File|*.xml|All Files|*.*");
savePlaylist.ShowDialog(); // Simply opens up the save file dialog window.
Write = new StreamWriter(savePlaylist.FileName);
for (int I = 0; I < lbPlaylist.Items.Count; I++)
{
Write.WriteLine(lbPlaylist.Items[I]);
}
Write.Close();
MessageBox.Show("Playlist saved!");
}
catch //(Exception ex)
{
return;
}
}
Please let me know your thoughts on my code. I am still a beginner and I find it hard to understand some of the codes I see online. Haha. Just want to make my Save Playlist and Create Playlist button work. Thanks.