wpf_player.LocalStoreWindow.addFile_Click C# (CSharp) Method

addFile_Click() private method

Add file button click handler. This method shows a file dialog and insert the track in the repository using the peer interface method.
private addFile_Click ( object sender, RoutedEventArgs e ) : void
sender object
e System.Windows.RoutedEventArgs
return void
        private void addFile_Click(object sender, RoutedEventArgs e)
        {
            this.QueryField.IsEnabled = false;
            LocalStoreModel vm = this.DataContext as LocalStoreModel;
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".mp3"; // Default file extension
            dlg.Filter = "Mp3 File (.mp3)|*.mp3"; // Filter files by extension
            dlg.Multiselect = true;

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string[] filenames = dlg.FileNames;
                int successCount = 0;
                List<string> failList = new List<string>();
                foreach (string fl in filenames)
                {
                    bool resp=vm.StoreFile(fl);
                    if (resp)
                    {
                        successCount++;
                    }
                    else
                    {
                        failList.Add(fl);
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(successCount);
                sb.Append((successCount == 1) ? " file has " : " files have ");
                sb.Append("been successfully added to the local music store");
                if (failList.Count > 0)
                {
                    sb.Append("\n");
                    sb.Append(failList.Count);
                    sb.Append((failList.Count == 1) ? " file is " : " files are ");
                    sb.Append(" already contained in the store.\n");
                    foreach (string fl in failList)
                    {
                        sb.Append(" - ");
                        sb.Append(fl);
                        sb.Append("\n");
                    }
                }
                MessageBox.Show(this, sb.ToString(), "Store Files", MessageBoxButton.OK, MessageBoxImage.Information);
                vm.RefreshList.Execute(null);
                vm.RefreshList.Execute(null);
                this.QueryField.IsEnabled = true;
            }
        }