Click here to Skip to main content
15,881,455 members
Articles / Programming Languages / C#

Code Template Add-In for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (71 votes)
6 May 200413 min read 464.1K   1.2K   150  
A Visual Studio .NET addin that provides a mechanism for inserting commonly used code snippets.
using System;
using EnvDTE;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Core;
using System.Runtime.InteropServices;

namespace CodeTemplate
{
	/// <summary>
	/// Summary description for InstallerSupport.
	/// </summary>
	[ComVisible(false)]
	[RunInstaller(true)]
	public class InstallerSupport : Installer
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public InstallerSupport()
		{
			// This call is required by the Designer.
			InitializeComponent();
		}

		/// <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 Component 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()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

		private static bool IsVisualStudioRunning()
		{
			System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

			try
			{
				foreach(System.Diagnostics.Process p in processes)
				{
					if(p != null && p.MainModule != null)
					{
						String fileName = Path.GetFileName(p.MainModule.FileName);
						if(String.Compare(fileName, "devenv.exe", true) == 0)
							return true;
					}
				}
			}
			catch
			{
			}

			return false;
		}
 
		public override void Install(IDictionary stateSaver)
		{
			Trace.WriteLine("Install...");

			while(IsVisualStudioRunning())
			{
				DialogResult res = MessageBox.Show(
					"A running instance of Visual Studio .NET was found.\n\n" + 
					"Please close all copies of Visual Studio .NET and press OK.",
					"Install...",
					MessageBoxButtons.OKCancel);

				if(res == DialogResult.Cancel)
					throw new InstallException("Installation cancelled by the user");
			}

			base.Install(stateSaver);
		}

		private void RemoveCommands(String progID)
		{
			DTE dte = null;

			try
			{
				Type type = Type.GetTypeFromProgID(progID);
				if(type == null)
					return;

				dte = Activator.CreateInstance(type, true) as DTE;
				if(dte == null)
					return;

				Commands cmds = dte.Commands;

				Command cmd = cmds.Item(Connect.ShowMenuCommand.FullCommand, -1);
				if(cmd != null)
					cmd.Delete();

				cmd = cmds.Item(Connect.InsertTemplateCommand.FullCommand, -1);
				if(cmd != null)
					cmd.Delete();

//TODO: Figure out a way to remove the darn commandbar
//				CommandBar cb = dte.CommandBars[Connect.ShowMenuCommand.ProgID];
//				if(cb != null)
//					cmds.RemoveCommandBar(cb);

			}
			catch(Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
			finally
			{
				if(dte != null)
					dte.Quit();
			}
		}

		public override void Uninstall(IDictionary savedState)
		{
			Trace.WriteLine("Uninstall...");

			while(IsVisualStudioRunning())
			{
				DialogResult res = MessageBox.Show(
					"A running instance of Visual Studio .NET was found.\n\n" + 
					"Please close all copies of Visual Studio .NET and press OK.",
					"Uninstall...",
					MessageBoxButtons.OKCancel);

				if(res == DialogResult.Cancel)
					throw new InstallException("Installation cancelled by the user");
			}
	
			RemoveCommands("VisualStudio.DTE.7");	// VS2002
			RemoveCommands("VisualStudio.DTE.7.1");	// VS2003

			base.Uninstall(savedState);
		}
	}
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions