Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C# 4.0

Gilma - GUI for the ILMerge Application Revised for .NET 4.0

Rate me:
Please Sign up or sign in to vote.
4.41/5 (15 votes)
13 Feb 2013CPOL1 min read 135.6K   9K   62  
An article on a GUI to Microsoft ILMerge Program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Gilma {
  public partial class MainForm : Form {

    private StateFile myStateFile = new StateFile();

    public MainForm() {
      InitializeComponent();
      ClearControls();
    }

    private void buttonCancel_Click(object sender, System.EventArgs e) {
      Close();
    }

    private void buttonBrowseStrongKeyLocation_Click(object sender, EventArgs e) {
      if ( textBoxStrongKeyLocation.Text != String.Empty ) {
        FileInfo pip = new FileInfo(textBoxStrongKeyLocation.Text);
        openFileDialogKeyFileLocation.InitialDirectory = pip.DirectoryName;
        openFileDialogKeyFileLocation.FileName = pip.Name;
      }
      openFileDialogKeyFileLocation.ShowDialog();
    }

    private void buttonBrowseOutputFilename_Click(object sender, System.EventArgs e) {
      if ( textBoxOutputFilename.Text != String.Empty ) {
        FileInfo pip = new FileInfo(textBoxOutputFilename.Text);
        openFileDialogOutputFilename.InitialDirectory = pip.DirectoryName;
        openFileDialogOutputFilename.FileName = pip.Name;
      }
      openFileDialogOutputFilename.ShowDialog();
    }

    private void buttonBrowsePrimaryAssembly_Click(object sender, System.EventArgs e) {
      if ( textBoxPrimaryAssembly.Text != String.Empty ) {
        FileInfo pip = new FileInfo(textBoxPrimaryAssembly.Text);
        openFilePrimaryAssembly.InitialDirectory = pip.DirectoryName;
        openFilePrimaryAssembly.InitialDirectory = pip.Name;
      }
      openFilePrimaryAssembly.ShowDialog();
    }

    private void openFileDialogOutputFilename_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
      textBoxOutputFilename.Text = openFileDialogOutputFilename.FileName;
    }

    private void openFilePrimaryAssembly_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
      textBoxPrimaryAssembly.Text = openFilePrimaryAssembly.FileName;
    }

    private void strongKeyCheckBox_CheckedChanged(object sender, EventArgs e) {
      this.textBoxStrongKeyLocation.Enabled = buttonBrowseStrongKeyLocation.Enabled = ((CheckBox)sender).Checked;
    }

    private void openFileDialogKeyFileLocation_FileOk(object sender, CancelEventArgs e) {
      textBoxStrongKeyLocation.Text = openFileDialogKeyFileLocation.FileName;
    }

    private void buttonAddAssemblies_Click(object sender, System.EventArgs e) {
      AddOtherAssembly();
    }

    private void buttonRemoveAssemblies_Click(object sender, System.EventArgs e) {
      RemoveSelectedOtherAssemblies();
    }

    private void openFileAddAssemblies_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
      foreach ( string filename in openFileAddAssemblies.FileNames ) {
        if ( !listBoxOtherAssembies.Items.Contains(filename) ) {
          listBoxOtherAssembies.Items.Add(filename);
        }
      }
    }

    private void saveFileDialogConfiguration_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
      SaveConfiguration(saveFileDialogConfiguration.FileName);
    }
    private void openFileDialogConfiguration_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
      LoadConfiguration(openFileDialogConfiguration.FileName);
    }

    private void buttonSave_Click(object sender, System.EventArgs e) {
      saveFileDialogConfiguration.ShowDialog();
    }

    private void buttonLoad_Click(object sender, System.EventArgs e) {
      openFileDialogConfiguration.ShowDialog();
    }

    private void buttonMerge_Click(object sender, System.EventArgs e) {
      Merge();
    }

    private void contextMenuOtherAssemblies_Popup(object sender, System.EventArgs e) {
      menuItemOtherAssembliesRemove.Enabled = listBoxOtherAssembies.SelectedItems.Count > 0;
    }

    private void menuItemOtherAssembliesAdd_Click(object sender, System.EventArgs e) {
      AddOtherAssembly();
    }

    private void menuItemOtherAssembliesRemove_Click(object sender, System.EventArgs e) {
      RemoveSelectedOtherAssemblies();
    }

    private void comboBoxTargetType_SelectedIndexChanged(object sender, EventArgs e) {
      int alfa = comboBoxTargetType.SelectedIndex;
      this.myStateFile.Type = (StateFile.TargetType)alfa;
    }

    private void buttonClear_Click(object sender, System.EventArgs e) {
      ClearControls();
    }

    private void RemoveSelectedOtherAssemblies() {
      foreach ( int index in listBoxOtherAssembies.SelectedIndices ) {
        listBoxOtherAssembies.Items.RemoveAt(index);
        this.myStateFile.OtherAssemblies.RemoveAt(index);
      }
    }

    private void AddOtherAssembly() {
      openFileAddAssemblies.ShowDialog();
    }

    private void LoadConfiguration(string filename) {
      myStateFile = StateFile.Load(filename);
      textBoxPrimaryAssembly.Text = myStateFile.PrimaryAssembly;
      if ( myStateFile.KeyFile.Length > 0 ) {
        this.strongKeyCheckBox.Checked = true;
        textBoxStrongKeyLocation.Text = myStateFile.KeyFile;
      } else {
        this.strongKeyCheckBox.Checked = true;
      }
      textBoxOutputFilename.Text = myStateFile.OutputFile;
      zeroPeKindCheckBox.Checked = myStateFile.ZeroPeKind;
      listBoxOtherAssembies.Items.Clear();
      foreach ( string myAssembly in myStateFile.OtherAssemblies ) {
        listBoxOtherAssembies.Items.Add(myAssembly);
      }
    }

    private void SaveConfiguration(string filename) {
      myStateFile.Save(filename);
    }

    private void ClearControls() {
      textBoxStrongKeyLocation.Text = String.Empty;
      comboBoxTargetType.DataSource = Enum.GetNames(typeof(StateFile.TargetType));
      checkBoxIncludePdb.Checked = false;
      zeroPeKindCheckBox.Checked = false;
      listBoxOtherAssembies.Items.Clear();
      textBoxOutputFilename.Text = String.Empty;
      textBoxPrimaryAssembly.Text = String.Empty;
    }

    private void Merge() {
      if ( textBoxOutputFilename.Text == String.Empty ) {
        m_ErrorProvider.SetError(textBoxOutputFilename, "Please enter the output filename!");
        return;
      }
      this.myStateFile.OutputFile = textBoxOutputFilename.Text;
      if ( textBoxPrimaryAssembly.Text == String.Empty ) {
        m_ErrorProvider.SetError(textBoxPrimaryAssembly, "Please enter the primary assembly filename!");
        return;
      }
      this.myStateFile.PrimaryAssembly = textBoxPrimaryAssembly.Text;
      if ( this.strongKeyCheckBox.Checked ) {
        if ( textBoxStrongKeyLocation.Text == String.Empty ) {
          m_ErrorProvider.SetError(textBoxStrongKeyLocation, "Please enter the strong key filename!");
          return;
        }
        this.myStateFile.KeyFile = textBoxStrongKeyLocation.Text;
      }
      this.myStateFile.OtherAssemblies.Clear();
      foreach ( string filename in listBoxOtherAssembies.Items ) {
        this.myStateFile.OtherAssemblies.Add(filename);
      }
      this.myStateFile.ZeroPeKind = this.zeroPeKindCheckBox.Checked;
      this.Enabled = false;
      this.Cursor = Cursors.WaitCursor;
      StringBuilder output = myStateFile.Merge();
      string outputString = output.ToString();
      if ( outputString.Contains("Error") ) {
        MessageBox.Show(outputString, "ILMerge output", MessageBoxButtons.OK, MessageBoxIcon.Information);
      } else {
        MergeResultsForm myPanel = new MergeResultsForm();
        myPanel.setContent(outputString);
        myPanel.ShowDialog(this);
      }
      this.Enabled = true;
      this.Cursor = Cursors.Default;
    }

    private void textBoxStrongKeyLocation_DragDrop(object sender, DragEventArgs e) {
      MyDragDrop(sender, e);
    }

    private void textBoxPrimaryAssembly_DragDrop(object sender, DragEventArgs e) {
      MyDragDrop(sender, e);
    }

    private void textBoxOutputFilename_DragDrop(object sender, DragEventArgs e) {
      MyDragDrop(sender, e);
    }

    private void MyDragDrop(object sender, DragEventArgs args) {
      string fileName = IsSingleFile(args);
      if ( fileName == null )
        return;
      (sender as TextBox).Text = fileName;
    }

    private void textBoxOutputFilename_DragOver(object sender, DragEventArgs e) {
      MyDragOver(e);
    }

    private void textBoxPrimaryAssembly_DragOver(object sender, DragEventArgs e) {
      MyDragOver(e);
    }

    private void textBoxStrongKeyLocation_DragOver(object sender, DragEventArgs e) {
      MyDragOver(e);
    }

    private void MyDragOver(DragEventArgs args) {
      if ( IsSingleFile(args) != null )
        args.Effect = DragDropEffects.Copy;
      else
        args.Effect = DragDropEffects.None;
    }

    // If the data object in args is a single file, this method will return the filename.
    // Otherwise, it returns null.
    private string IsSingleFile(DragEventArgs args) {
      // Check for files in the hovering data object.
      if ( args.Data.GetDataPresent(DataFormats.FileDrop, true) ) {
        string[] fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[];
        // Check for a single file or folder.
        if ( fileNames.Length == 1 ) {
          // Check for a file (a directory will return false).
          if ( File.Exists(fileNames[0]) ) {
            // At this point we know there is a single file.
            return fileNames[0];
          }
        }
      }
      return null;
    }

    private void listBoxOtherAssembies_DragOver(object sender, DragEventArgs e) {
      e.Effect = DragDropEffects.Copy;
    }

    private void listBoxOtherAssembies_DragDrop(object sender, DragEventArgs e) {
      if ( e.Data.GetDataPresent(DataFormats.FileDrop, true) ) {
        foreach ( string filename in (e.Data.GetData(DataFormats.FileDrop, true) as string[]) ) {
          if ( !listBoxOtherAssembies.Items.Contains(filename) ) {
            listBoxOtherAssembies.Items.Add(filename);
          }
        }
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions