private void ImportPlaylistBtn_Click(object sender, RoutedEventArgs e) { var screen = new OpenFileDialog(); if (screen.ShowDialog() == true) { var filename = screen.FileName; if (String.IsNullOrEmpty(filename)) { return; } using (StreamReader reader = new StreamReader(filename, Encoding.Default, true)) { string currentLine = reader.ReadLine(); var tokens = currentLine.Split(new string[] { "|" }, StringSplitOptions.None); string name = tokens[0]; if (!String.IsNullOrEmpty(name)) { BindingList <Media> medias = new BindingList <Media>(); if (tokens.Length > 1) { for (int i = 1; i < tokens.Length; i++) { if (!System.IO.File.Exists(tokens[i])) { continue; } Media media = new Media(tokens[i], name); medias.Add(media); } } for (int i = 0; i < playlists.Count; i++) { if (name == playlists[i].Name) { MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show($"This playlist name: \"{name}\" is already taken. Do you want to rename it?", "Rename Confirmation", System.Windows.MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { RenameDialog inputDialog = new RenameDialog(); if (inputDialog.ShowDialog() == true) { String newName = inputDialog.Answer; if (String.IsNullOrEmpty(newName)) { MessageBox.Show("Name must not be empty!"); return; } for (int j = 0; j < playlists.Count; j++) { if (newName == playlists[i].Name) { MessageBox.Show("This name is already taken"); return; } } Playlist _playlist = new Playlist { Medias = medias, Name = newName }; playlists.Add(_playlist); MessageBox.Show("Playlist is added!"); } } return; } } Playlist playlist = new Playlist { Medias = medias, Name = name }; playlists.Add(playlist); MessageBox.Show("Playlist is added!"); } } } }
private void getPlaylists() { playlists = new BindingList <Playlist>(); if (System.IO.File.Exists("file.txt")) { try { using (StreamReader reader = new StreamReader("file.txt", Encoding.Default, true)) { string firstLine = reader.ReadLine(); if (String.IsNullOrEmpty(firstLine)) { playDefaultMusic(); playlistsListBox.ItemsSource = playlists; return; } var tokensLastsong = firstLine.Split(new string[] { "|" }, StringSplitOptions.None); string currentLine; while ((currentLine = reader.ReadLine()) != null) { var tokens = currentLine.Split(new string[] { "|" }, StringSplitOptions.None); string name = tokens[0]; if (!String.IsNullOrEmpty(name)) { BindingList <Media> medias = new BindingList <Media>(); if (tokens.Length > 1) { for (int i = 1; i < tokens.Length; i++) { if (!System.IO.File.Exists(tokens[i])) { continue; } Media media = new Media(tokens[i], name); medias.Add(media); } } Playlist playlist = new Playlist { Medias = medias, Name = name }; playlists.Add(playlist); } } getLastSongPlaying(tokensLastsong[0], tokensLastsong[1], tokensLastsong[2]); } } catch (Exception err) { MessageBox.Show("Read file save error!\n" + err.Message, "Error"); playDefaultMusic(); } } else { playDefaultMusic(); } playlistsListBox.ItemsSource = playlists; }