private void button1_Click(object sender, EventArgs e) { bool b = true; axWindowsMediaPlayer1.Ctlcontrols.stop(); Song newSong = new Song(); BinaryFormatter bf = new BinaryFormatter(); FileStream f = new FileStream("Files/Songs/songs.dat", FileMode.OpenOrCreate); List <Song> list; try { list = (List <Song>)bf.Deserialize(f); } catch { list = new List <Song>(); } f.Close(); if (adding) { foreach (Song s in list) { if (s.SongName == name) { DialogResult res = MessageBox.Show("Song with this name already exists !\nDo you want to change the name of this song ?", "Error", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { InputPopUp inp = new InputPopUp("Give the new Name of the song"); inp.ShowDialog(); if (inp.change) { b = true; name = inp.name; button1_Click(sender, e); } } else if (res == DialogResult.No) { b = false; break; } } } } if (b && adding) { newSong.Path = textBox1.Text; newSong.SongName = name; newSong.SongLength = length; newSong.ArtistName = textBox2.Text; newSong.MusicType = textBox3.Text; newSong.Language = textBox4.Text; newSong.PublishYear = textBox5.Text; newSong.Image = pictureBox1.Image; list.Add(newSong); BinaryFormatter bf1 = new BinaryFormatter(); FileStream f1 = new FileStream("Files/Songs/songs.dat", FileMode.Create); bf1.Serialize(f1, list); f1.Close(); MessageBox.Show("Song successfully added"); this.Close(); } else if (!adding) //Else if editing { if (list.Count() != 0) { list[selectedIndex].ArtistName = textBox2.Text; list[selectedIndex].MusicType = textBox3.Text; list[selectedIndex].Language = textBox4.Text; list[selectedIndex].PublishYear = textBox5.Text; list[selectedIndex].Image = pictureBox1.Image; BinaryFormatter bf1 = new BinaryFormatter(); FileStream f1 = new FileStream("Files/Songs/songs.dat", FileMode.Create); bf1.Serialize(f1, list); f1.Close(); MessageBox.Show("Song successfully edited"); this.Close(); } else { MessageBox.Show("Oops , something happened and we couldn't edit the song"); } } }
private void Create(object sender, EventArgs e) { indexList = ap.indexList; if (indexList.Count() != 0) { InputPopUp inp = new InputPopUp("Give the Name of the playlist"); inp.ShowDialog(); string name = inp.name; if (name != "") { List <Song> tmp; BinaryFormatter bf = new BinaryFormatter(); FileStream f = new FileStream("Files/Playlists/playlists.dat", FileMode.OpenOrCreate); try { tmp = (List <Song>)bf.Deserialize(f); } catch { tmp = new List <Song>(); } f.Close(); bool t = true; foreach (Song s in tmp) { if (name == s.SongName) { MessageBox.Show("Name already exists"); t = false; break; } } if (t) { playlist = new List <Song>(); foreach (int i in indexList) { playlist.Add(songList[i]); } BinaryFormatter bf1 = new BinaryFormatter(); FileStream f1 = new FileStream("Files/Playlists/" + name + ".dat", FileMode.Create); bf1.Serialize(f1, playlist); f1.Close(); Song nL = new Song(); nL.Path = "Files/Playlists/" + name + ".dat"; nL.SongName = name; nL.Image = new Bitmap("Files/Pictures/Default_Playlist.png"); List <Song> tmpList; FileStream f2 = new FileStream("Files/Playlists/playlists.dat", FileMode.OpenOrCreate); try { tmpList = (List <Song>)bf1.Deserialize(f2); } catch { tmpList = new List <Song>(); } f2.Close(); tmpList.Add(nL); FileStream f3 = new FileStream("Files/Playlists/playlists.dat", FileMode.Create); bf1.Serialize(f3, tmpList); f3.Close(); this.Close(); } } } else { MessageBox.Show("Select at least one song"); } }