示例#1
0
        /// <summary>
        /// Adds the selected items in the local list box to the database.
        /// </summary>
        /// <param name="sender">Automatically generated by Visual Studio.</param>
        /// <param name="e">Automatically generated by Visual Studio.</param>
        private void toDBButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (MPAiModel DBModel = new MPAiModel())
                {
                    DialogResult renameAction = MPAiMessageBoxFactory.Show(renamewarningText,
                                                                           warningText, MPAiMessageBoxButtons.OKCancel);
                    // If the user selected cancel, don't take any action.
                    if (renameAction.Equals(DialogResult.Cancel))
                    {
                        return;
                    }
                    foreach (FileInfo item in mediaLocalListBox.SelectedItems)  // For each selected item...
                    {
                        FileInfo workingFile = item;

                        // Need to rename file.
                        // If the user wanted to rename them themselves, take the same action as in SpeechRecognitionTest - automatically bring up rename.

                        if (!NameParser.IsFileNameCorrect(workingFile.Name))
                        {
                            // Back up the file to a temporary folder.
                            File.Copy(workingFile.FullName, Path.Combine(AppDataPath.Temp, "Rename_Backup"));

                            //Open the rename dialog
                            RenameFileDialog renameDialog = new RenameFileDialog(workingFile.FullName, true);
                            if (renameDialog.ShowDialog(this).Equals(DialogResult.OK))
                            {
                                // The old file has been changed to this.
                                FileInfo renamedFile = renameDialog.RenamedFile;
                                // Restore the old file, with old name intact, from the backup.
                                File.Move(Path.Combine(AppDataPath.Temp, "Rename_Backup"), workingFile.FullName);
                                // Continue the process with the new file name.
                                workingFile = renamedFile;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        // If the file follows convention (i.e. parts.length == 4), do nothing.

                        NameParser parser = new NameParser();
                        parser.FullName = workingFile.FullName;          // Put the name into the parser
                        // Set the parser address to the audio or video folder as appropriate.
                        if (parser.MediaFormat == "audio")
                        {
                            parser.Address = DirectoryManagement.AudioFolder;
                        }
                        else if (parser.MediaFormat == "video")
                        {
                            parser.Address = DirectoryManagement.VideoFolder;
                        }
                        // Get the file and add it to the database context.
                        DBModel.AddOrUpdateRecordingFile(parser.SingleFile);
                        // Copy the existing local file into the audio/video folder if it wasn't already there.
                        string existingFile = workingFile.FullName;
                        string newFile      = Path.Combine(parser.Address, workingFile.Name);
                        if (!existingFile.Equals(newFile))
                        {
                            File.Copy(existingFile, newFile, true);
                        }
                        DBModel.SaveChanges();
                    }
                }
                populateListBox();
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.StackTrace);
                MPAiMessageBoxFactory.Show(exp.StackTrace);
            }
            finally
            {
                File.Delete(Path.Combine(AppDataPath.Temp, "Rename_Backup"));
            }
        }