private void AddSongs_bt_Click_1(object sender, EventArgs e) { if (File.Exists("AllSongs.xml")) { FileStream fs = new FileStream("AllSongs.xml", FileMode.OpenOrCreate); XmlSerializer xs = new XmlSerializer(songs_list.GetType()); songs_list = (List <Song>)xs.Deserialize(fs); fs.Close(); } OpenFileDialog dia = new OpenFileDialog(); if (dia.ShowDialog() == System.Windows.Forms.DialogResult.OK) { String strfilename = dia.FileName; string sourceFile = strfilename; songname = Path.GetFileName(sourceFile); string destinationFile = @"C:\Users\Mido\Desktop\Musically\My Playlist\My Playlist\Resources\AllSongs\" + songname; try { File.Copy(sourceFile, Path.Combine(destinationFile), true); } catch (Exception) { MessageBox.Show("We are very sorry , try to add another song"); } mp3reader m = new mp3reader(destinationFile); Song c = m.GetTag(); c.path = destinationFile; songs_list.Add(c); FileStream fs = new FileStream("AllSongs.xml", FileMode.OpenOrCreate); XmlSerializer xs = new XmlSerializer(songs_list.GetType()); xs.Serialize(fs, songs_list); fs.Close(); SongsPanel.Controls.Clear(); names = new string[songs_list.Count]; paths = new string[songs_list.Count]; for (int i = 0; i < songs_list.Count; i++) { string titleval = songs_list[i].name; string pathval = songs_list[i].path; sname = titleval; path = pathval; paths[i] = pathval; names[i] = titleval; User_controllers.Songaya ss = new User_controllers.Songaya(); ss.songnamelbl.Text = sname; SongsPanel.Controls.Add(ss); } } }
private void AddSongs_bt_Click_1(object sender, EventArgs e) { OpenFileDialog dia = new OpenFileDialog(); if (dia.ShowDialog() == System.Windows.Forms.DialogResult.OK) { String strfilename = dia.FileName; string sourceFile = strfilename; songname = Path.GetFileName(sourceFile); string destinationFile = @"F:\projects\Musically\My Playlist\My Playlist\Resources\AllSongs\" + songname; try { File.Copy(sourceFile, Path.Combine(destinationFile), true); } catch (Exception) { MessageBox.Show("We are very sorry , try to add another song"); } mp3reader m = new mp3reader(destinationFile); Song c = m.GetTag(); if (!File.Exists("AllSongs.xml")) { XmlWriter writer = XmlWriter.Create("AllSongs.xml"); writer.WriteStartDocument(); writer.WriteStartElement("AllSongs"); writer.WriteStartElement("Song"); writer.WriteStartElement("Title"); writer.WriteString(c.name); writer.WriteEndElement(); writer.WriteStartElement("Artist"); writer.WriteString(c.Artist); writer.WriteEndElement(); writer.WriteStartElement("Album"); writer.WriteString(c.Album); writer.WriteEndElement(); writer.WriteStartElement("Path"); writer.WriteString(destinationFile); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); } else { XmlDocument doc2 = new XmlDocument(); XmlElement son = doc2.CreateElement("Song"); XmlElement node = doc2.CreateElement("Title"); node.InnerText = c.name; son.AppendChild(node); node = doc2.CreateElement("Artist"); node.InnerText = c.Artist; son.AppendChild(node); node = doc2.CreateElement("Album"); node.InnerText = c.Album; son.AppendChild(node); node = doc2.CreateElement("Path"); node.InnerText = destinationFile; son.AppendChild(node); doc2.Load("AllSongs.xml"); XmlElement root = doc2.DocumentElement; root.AppendChild(son); doc2.Save("AllSongs.xml"); } //listBox1.Items.Clear(); SongsPanel.Controls.Clear(); XmlDocument doc = new XmlDocument(); doc.Load("AllSongs.xml"); XmlNodeList list = doc.GetElementsByTagName("Song"); names = new string[list.Count]; paths = new string[list.Count]; for (int i = 0; i < list.Count; i++) { XmlNodeList child = list[i].ChildNodes; string titleval = child[0].InnerText; string artistval = child[1].InnerText; string albumval = child[2].InnerText; string pathval = child[3].InnerText; sname = titleval; path = pathval; paths[i] = pathval; names[i] = titleval; User_controllers.Songaya s = new User_controllers.Songaya(); s.songnamelbl.Text = sname; SongsPanel.Controls.Add(s); } } }