Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Create conditional shortcuts during the installation of a .NET application

, 22 Jan 2006
This article shows how to add shortcuts during an installation, based on the user's choice.
shortcut_demo.zip
Shortcut_demo
Shortcut to Test.lnk
Shortcut.exe
Test1.lnk
Test2.lnk
shortcut_src.zip
Shortcut_src
App.ico
Shortcut.csproj.user
Icons
banner1.jpg
setup.ico
HOMEPAGE.ICO
Test2.lnk
Shortcut to Test.lnk
Test1.lnk
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace Shortcut
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		# region Declarations 
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnNext;
		private System.Windows.Forms.CheckBox cbDesktop;
		private System.Windows.Forms.CheckBox cbStartMenu;
		private System.Windows.Forms.CheckBox cbQuickLaunch;
		private System.Windows.Forms.Button btnCancel;
		# endregion
		private System.Windows.Forms.ToolTip toolTip1;
		private System.ComponentModel.IContainer components;

		# region Constructor 
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		# endregion

		# region Destructor 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// 
	
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		# endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cbQuickLaunch = new System.Windows.Forms.CheckBox();
			this.cbStartMenu = new System.Windows.Forms.CheckBox();
			this.cbDesktop = new System.Windows.Forms.CheckBox();
			this.btnNext = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(496, 68);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cbQuickLaunch);
			this.groupBox1.Controls.Add(this.cbStartMenu);
			this.groupBox1.Controls.Add(this.cbDesktop);
			this.groupBox1.Location = new System.Drawing.Point(0, 64);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(504, 272);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			// 
			// cbQuickLaunch
			// 
			this.cbQuickLaunch.Checked = true;
			this.cbQuickLaunch.CheckState = System.Windows.Forms.CheckState.Checked;
			this.cbQuickLaunch.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cbQuickLaunch.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cbQuickLaunch.Location = new System.Drawing.Point(112, 172);
			this.cbQuickLaunch.Name = "cbQuickLaunch";
			this.cbQuickLaunch.Size = new System.Drawing.Size(304, 24);
			this.cbQuickLaunch.TabIndex = 2;
			this.cbQuickLaunch.Text = "Do you want to create shortcut in Quick Launch?";
			// 
			// cbStartMenu
			// 
			this.cbStartMenu.Checked = true;
			this.cbStartMenu.CheckState = System.Windows.Forms.CheckState.Checked;
			this.cbStartMenu.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cbStartMenu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cbStartMenu.Location = new System.Drawing.Point(112, 124);
			this.cbStartMenu.Name = "cbStartMenu";
			this.cbStartMenu.Size = new System.Drawing.Size(296, 24);
			this.cbStartMenu.TabIndex = 1;
			this.cbStartMenu.Text = "Do you want to create shortcut in Start Menu?";
			// 
			// cbDesktop
			// 
			this.cbDesktop.Checked = true;
			this.cbDesktop.CheckState = System.Windows.Forms.CheckState.Checked;
			this.cbDesktop.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cbDesktop.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cbDesktop.Location = new System.Drawing.Point(112, 76);
			this.cbDesktop.Name = "cbDesktop";
			this.cbDesktop.Size = new System.Drawing.Size(280, 24);
			this.cbDesktop.TabIndex = 0;
			this.cbDesktop.Text = "Do you want to create shortcut on Desktop?";
			// 
			// btnNext
			// 
			this.btnNext.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnNext.Location = new System.Drawing.Point(400, 352);
			this.btnNext.Name = "btnNext";
			this.btnNext.Size = new System.Drawing.Size(88, 23);
			this.btnNext.TabIndex = 2;
			this.btnNext.Text = "Next";
			this.toolTip1.SetToolTip(this.btnNext, "Clicking this button will create shortcuts in respective folders ");
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(304, 352);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 23);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			this.toolTip1.SetToolTip(this.btnCancel, "Clicking this button will remove all shortcuts");
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(496, 390);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnNext);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Create Shortcuts";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		# region Main() Method 
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.DoEvents();
			Application.Run(new Form1());
		}
		
# endregion

		# region Create Shortcuts 
		
		/*This will actually move the shortcut files from 
		application folder to respective destination*/

		private void btnNext_Click(object sender, System.EventArgs e)
		{
			try
			{
				// Create shortcut on Desktop
				if(cbDesktop.Checked==true)
				{
					if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk")==false)
					{
						File.Move(Application.StartupPath+"\\Shortcut to Test.lnk",Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk");
					}
				}

				// Create shortcut in programs menu.
				 if(cbStartMenu.Checked==true)
				{
					 if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs).Trim()+"\\Test.lnk")==false)
					 {
						 File.Move(Application.StartupPath+"\\Test1.lnk",Environment.GetFolderPath(Environment.SpecialFolder.Programs).Trim()+"\\Test.lnk");
					 }
				}

				// Create shortcut in Quick Launch Toolbar
				 if(cbQuickLaunch.Checked==true)
				{
					 if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"\\Microsoft\\Internet Explorer\\Quick Launch\\Test.lnk")==false)
					 {
						 File.Move(Application.StartupPath+"\\Test2.lnk",Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"\\Microsoft\\Internet Explorer\\Quick Launch\\Test.lnk");
					 }
				}
				//btnNext.Enabled=false;
				//btnCancel.Enabled=true;
				
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

# endregion

		# region Remove Shortcuts 
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk")==true)
				{
					File.Move(Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk",Application.StartupPath+"\\Shortcut to Test.lnk");
				}
				if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Programs).Trim()+"\\Test.lnk")==true)
				{
					File.Move(Environment.GetFolderPath(Environment.SpecialFolder.Programs).Trim()+"\\Test.lnk",Application.StartupPath+"\\Test1.lnk");
                }
				if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"\\Microsoft\\Internet Explorer\\Quick Launch\\Test.lnk")==true)
				{
					File.Move(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"\\Microsoft\\Internet Explorer\\Quick Launch\\Test.lnk",Application.StartupPath+"\\Test2.lnk");
				}
				//btnNext.Enabled=true;
				//btnCancel.Enabled=false;
				
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		# endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

*****

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 23 Jan 2006
Article Copyright 2005 by Mukund Pujari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid