Click here to Skip to main content
15,896,912 members
Articles / Desktop Programming / Windows Forms

Self-Extractor

Rate me:
Please Sign up or sign in to vote.
4.89/5 (37 votes)
31 Jul 2009CPOL4 min read 91K   2K   106  
How to embed resources at runtime by creating dynamic assemblies.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ArchiveCompiler.Demo
{
	public partial class Form1 : Form
	{
		BindingList<IconFileInfo> files = new BindingList<IconFileInfo>();

		public Form1()
		{
			InitializeComponent();

			pictureBox1.Image = Icon.ExtractAssociatedIcon(Application.ExecutablePath).ToBitmap();

			dataGridView1.AutoGenerateColumns = false;
			dataGridView1.DataSource = files;
		}

		private void Form1_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
				e.Effect = DragDropEffects.Copy;
		}

		private void Form1_DragDrop(object sender, DragEventArgs e)
		{
			foreach (string filename in (string[])e.Data.GetData(DataFormats.FileDrop))
			{
				files.Add(new IconFileInfo(filename));
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK != saveFileDialog1.ShowDialog(this))
				return;

			try
			{
				using (SelfExtractor archive = new SelfExtractor())
				{
					foreach (IconFileInfo file in files)
					{
						archive.AddFile(file.FullName);
					}
					archive.CompileArchive(saveFileDialog1.FileName, checkBox1.Checked, pictureBox1.ImageLocation);
				}

				MessageBox.Show(this, Path.GetFileName(saveFileDialog1.FileName) + " built successfully.",
					Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK != openFileDialog1.ShowDialog(this))
				return;

			pictureBox1.ImageLocation = openFileDialog1.FileName;
		}
	}

	class IconFileInfo
	{
		FileInfo fi;
		Icon icon;

		public IconFileInfo(string filename)
		{
			fi = new FileInfo(filename);
			icon = Icon.ExtractAssociatedIcon(filename);
		}

		public Icon Icon { get { return icon; } }
		public string Name { get { return fi.Name; } }
		public long Size { get { return fi.Length; } }
		public string FullName { get { return fi.FullName; } }
		public DateTime Date { get { return fi.LastWriteTime; } }
	}
}

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
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions