From 119c19cfdc716d173de364e9fa343f6d8d005b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20G=C3=B3recki?= Date: Mon, 23 Jul 2018 21:56:24 +0200 Subject: [PATCH] When no output filename specified it tries to generate one. For input xxx.yyy the output will be xxx.webm. For input xxx.webm the output will be xxx.webm.webm. Shows additional prompt to overwrite when a file with generated filename already exists. --- formMain.cs | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/formMain.cs b/formMain.cs index e64c54c..90c8ff6 100644 --- a/formMain.cs +++ b/formMain.cs @@ -81,10 +81,22 @@ private void btnConvert_Click(object sender, EventArgs e) } // Validates if the user input a value for txtOutput - if (txtOutput.Text == "") + if (txtOutput.Text == "" && txtInput.Text != "") { - verified = false; - MessageBox.Show("An output file needs to be selected", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + if (!txtInput.Text.EndsWith(".webm")) + { + // Keep filename, but change extension to "webm" + txtOutput.Text = txtInput.Text.Substring(0, txtInput.Text.LastIndexOf(".")) + ".webm"; + if (!overwriteExistingFileIfExists()) + { + verified = false; + } + } + else + { + // Add another extension to avoid overwriting original file. + txtOutput.Text = txtInput.Text + ".webm"; + } } // Validates if the user input a value for txtTimeStart @@ -450,5 +462,29 @@ private void formMain_DragDrop(object sender, DragEventArgs e) txtInput.Text = files[0]; } + + /// + /// If file with automatically generated filename exists show prompt to make user aware of that and let him decide if he wants to overwrite it. + /// + /// + private bool overwriteExistingFileIfExists() + { + FileInfo fi = new FileInfo(txtOutput.Text); + if (fi.Exists) + { + DialogResult dialogResult = MessageBox.Show("Overwrite " + txtOutput.Text + "?", "Output file exists", MessageBoxButtons.YesNo); + if (dialogResult == DialogResult.No) + { + saveFileDialog1.FileName = txtOutput.Text; + if (saveFileDialog1.ShowDialog() == DialogResult.OK) + { + txtOutput.Text = saveFileDialog1.FileName; + return true; + } + return false; + } + } + return true; + } } }