Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#

Creating Shell Links (Shortcuts) in .NET Programs Using WSH

Rate me:
Please Sign up or sign in to vote.
4.83/5 (33 votes)
3 Apr 20033 min read 362.8K   9.2K   89  
It's easy to create shortcuts using the Windows Script Host Object Model
// =====================================================================
//
// ShellLink - Using WSH to program shell links
//
// by Jim Hollenhorst, jwtk@ultrapico.com
// Copyright Ultrapico, April 2003
// http://www.ultrapico.com
//
// =====================================================================
using System;
using System.Windows.Forms;
using System.Drawing;

namespace ShellLinks
{
	/// <summary>
	/// This is the shell links demonstration application
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Button BtnExit;
		private bool Skip;
		private System.Windows.Forms.CheckBox chkRunOnStartup;
		private System.Windows.Forms.CheckBox chkSendToLink;
		private System.Windows.Forms.CheckBox chkQuickLaunch;
		private System.Windows.Forms.CheckBox chkDesktopLink;
		private string QuickLaunchDir;


		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Set check buttons depending on whether shortcuts exist on the desktop and in the startup folder
			Skip=true;  // Don't run the CheckedChanged code
			chkRunOnStartup.Checked=Link.Exists(Environment.SpecialFolder.Startup,"Shell Link");
			chkDesktopLink.Checked=Link.Exists(Environment.SpecialFolder.DesktopDirectory,"Shell Link");
			chkSendToLink.Checked=Link.Exists(Environment.SpecialFolder.SendTo,"Shell Link");
			QuickLaunchDir=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
				+ "\\Microsoft\\Internet Explorer\\Quick Launch";
			chkQuickLaunch.Checked=Link.Exists(QuickLaunchDir,"Shell Link");
			Skip=false;
		}

		public void Exit()
		{
			Application.Exit();
		}

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

		#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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.chkDesktopLink = new System.Windows.Forms.CheckBox();
			this.chkRunOnStartup = new System.Windows.Forms.CheckBox();
			this.chkQuickLaunch = new System.Windows.Forms.CheckBox();
			this.BtnExit = new System.Windows.Forms.Button();
			this.chkSendToLink = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// chkDesktopLink
			// 
			this.chkDesktopLink.Checked = true;
			this.chkDesktopLink.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkDesktopLink.Location = new System.Drawing.Point(8, 64);
			this.chkDesktopLink.Name = "chkDesktopLink";
			this.chkDesktopLink.Size = new System.Drawing.Size(184, 24);
			this.chkDesktopLink.TabIndex = 3;
			this.chkDesktopLink.Text = "Put Shortcut on the Desktop";
			this.chkDesktopLink.CheckedChanged += new System.EventHandler(this.chkDesktopLink_CheckedChanged);
			// 
			// chkRunOnStartup
			// 
			this.chkRunOnStartup.Checked = true;
			this.chkRunOnStartup.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkRunOnStartup.Location = new System.Drawing.Point(8, 40);
			this.chkRunOnStartup.Name = "chkRunOnStartup";
			this.chkRunOnStartup.Size = new System.Drawing.Size(176, 24);
			this.chkRunOnStartup.TabIndex = 2;
			this.chkRunOnStartup.Text = "Start Automatically at Login";
			this.chkRunOnStartup.CheckedChanged += new System.EventHandler(this.chkRunOnStartup_CheckedChanged);
			// 
			// chkQuickLaunch
			// 
			this.chkQuickLaunch.Checked = true;
			this.chkQuickLaunch.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkQuickLaunch.Location = new System.Drawing.Point(8, 16);
			this.chkQuickLaunch.Name = "chkQuickLaunch";
			this.chkQuickLaunch.Size = new System.Drawing.Size(176, 24);
			this.chkQuickLaunch.TabIndex = 1;
			this.chkQuickLaunch.Text = "Show in Quick Launch Area";
			this.chkQuickLaunch.CheckedChanged += new System.EventHandler(this.chkQuickLaunch_CheckedChanged);
			// 
			// BtnExit
			// 
			this.BtnExit.BackColor = System.Drawing.SystemColors.Control;
			this.BtnExit.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.BtnExit.DialogResult = System.Windows.Forms.DialogResult.Abort;
			this.BtnExit.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.BtnExit.Location = new System.Drawing.Point(192, 48);
			this.BtnExit.Name = "BtnExit";
			this.BtnExit.Size = new System.Drawing.Size(48, 23);
			this.BtnExit.TabIndex = 11;
			this.BtnExit.Text = "E&xit";
			this.BtnExit.Click += new System.EventHandler(this.BtnExit_Click);
			// 
			// chkSendToLink
			// 
			this.chkSendToLink.Checked = true;
			this.chkSendToLink.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkSendToLink.Location = new System.Drawing.Point(8, 88);
			this.chkSendToLink.Name = "chkSendToLink";
			this.chkSendToLink.Size = new System.Drawing.Size(160, 24);
			this.chkSendToLink.TabIndex = 12;
			this.chkSendToLink.Text = "Add to SendTo Menu";
			this.chkSendToLink.CheckedChanged += new System.EventHandler(this.chkSendToLink_CheckedChanged);
			// 
			// MainForm
			// 
			this.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.BtnExit;
			this.CausesValidation = false;
			this.ClientSize = new System.Drawing.Size(258, 127);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.chkSendToLink,
																		  this.BtnExit,
																		  this.chkDesktopLink,
																		  this.chkRunOnStartup,
																		  this.chkQuickLaunch});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MainForm";
			this.Text = "Demonstrate Shell Links";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}



		public void BtnExit_Click(object sender, System.EventArgs e)
		{
			Exit();
		}

		
		/// <summary>
		/// This method checks the startup directory to see if there is a link to the executable file
		/// it modifies the directory accordingly depending on the setting of the RunOnStartup checkbox 
		/// </summary>
		private void chkRunOnStartup_CheckedChanged(object sender, System.EventArgs e)
		{
			if(Skip)return;
			Link.Update(Environment.SpecialFolder.Startup,Application.ExecutablePath,"Shell Link",chkRunOnStartup.Checked);
		}

		// Update a link to the executable on the desktop depending on the setting of chkDesktopLink
		private void chkDesktopLink_CheckedChanged(object sender, System.EventArgs e)
		{
			if(Skip)return;
			Link.Update(Environment.SpecialFolder.DesktopDirectory,Application.ExecutablePath,"Shell Link",chkDesktopLink.Checked);
		}

		private void chkSendToLink_CheckedChanged(object sender, System.EventArgs e)
		{
			if(Skip)return;
			Link.Update(Environment.SpecialFolder.SendTo,Application.ExecutablePath,"Shell Link",chkSendToLink.Checked);
		}

		private void chkQuickLaunch_CheckedChanged(object sender, System.EventArgs e)
		{
			if(Skip)return;
			Link.Update(QuickLaunchDir,Application.ExecutablePath,"Shell Link",chkQuickLaunch.Checked);
		}
	}
}

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


Written By
Researcher
United States United States
Ultrapico Website: http://www.ultrapico.com

Download Expresso 3.0, the latest version of the award-winning regular expression development tool.

Comments and Discussions