CED.MainForm.runButton_Click C# (CSharp) Method

runButton_Click() private method

private runButton_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void runButton_Click(object sender, EventArgs e)
        {
            if (ValidateFormData(optionCheckBox.Checked))
            {
                errorProviderConfigSection.Clear();
                errorProviderCustomProvider.Clear();
                errorProviderFile.Clear();

                outputTextBox.Text = "";
                var tempDirectoryName = Guid.NewGuid().ToString();

                var configFileName = Path.GetFileName(configFileOpenDialog.FileName);
                var transformFileName = Path.GetFileName(transformFileOpenDialog.FileName);

                Directory.CreateDirectory(tempDirectoryName);

                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = "cmd.exe";

                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;

                startInfo.WorkingDirectory = tempDirectoryName;

                Settings.Default.ConfigSectionsHistory = AddToCollection(Settings.Default.ConfigSectionsHistory,
                    configurationSectionTextBox);
                Settings.Default.CustomProvidersHistory = AddToCollection(Settings.Default.CustomProvidersHistory,
                    customProviderTextBox);
                Settings.Default.Save();

                // Working with config transformations
                if (optionCheckBox.Checked)
                {
                    var transformedFilePath = Path.GetFullPath($"{tempDirectoryName}\\web.config");

                    startInfo.Arguments =
                        $"/C {Environment.CurrentDirectory}\\ctt.exe \"source: \"{configFileTextBox.Text}\"\" \"transform: \"{transformFileTextBox.Text}\"\" \"destination: \"{transformedFilePath}\"\"";

                    process.StartInfo = startInfo;
                    process.Start();
                    process.WaitForExit();

                    FormatDocument(tempDirectoryName);
                }
                else
                {
                    File.Copy(configFileOpenDialog.FileName, $"{tempDirectoryName}\\web.config");
                }

                if (encryptRadioButton.Checked)
                {
                    startInfo.Arguments =
                        $"/C {System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()}\\aspnet_regiis -pef \"{configurationSectionTextBox.Text}\" \".\" -prov \"{customProviderTextBox.Text}\"";
                }
                else
                {
                    startInfo.Arguments =
                        $"/C {System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()}\\aspnet_regiis -pdf \"{configurationSectionTextBox.Text}\" \".\"";
                }

                process.StartInfo = startInfo;
                process.Start();

                var output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                // this works with English Windows, it might be different for a different localization
                if (output.EndsWith("Succeeded!\r\n"))
                {
                    outputTextBox.Text = System.IO.File.ReadAllText($"{tempDirectoryName}\\web.config");
                }
                else
                {
                    outputTextBox.Text = output;
                }

                try
                {
                    Directory.Delete($"{tempDirectoryName}", true);
                }
                catch (Exception)
                {
                    // cannot delete temp folder, ignore
                }
            }
            else
            {
                MessageBox.Show("All the fields are required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }