Skip to content

Instantly share code, notes, and snippets.

@tillig
Last active January 4, 2017 21:15
Show Gist options
  • Save tillig/c5ec9e52b80329f76dbc84b745c5a5d5 to your computer and use it in GitHub Desktop.
Save tillig/c5ec9e52b80329f76dbc84b745c5a5d5 to your computer and use it in GitHub Desktop.
Example console app showing how to add items to the Visual Studio toolbox
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ToolboxIntegration
{
class InstallApp
{
protected const string DTE_PROG_ID = "VisualStudio.DTE.7.1";
protected const string WEB_DLL_PATH = @"C:\path\to\controls.dll";
protected const string TAB_NAME_FORMAT = "My Controls {0}.{1}.{2}";
protected static EnvDTE.DTE dte = null;
protected static string toolboxTabName = "";
[STAThread]
private static void Main(string[] args)
{
try{
InitializeEnvironment();
if(args.Length == 1){
switch(args[0]){
case "add":
AddItemsToToolbox();
break;
case "remove":
RemoveToolboxTab();
break;
case "enum":
EnumToolboxTabs();
break;
default:
ShowUsage();
break;
}
}
else{
ShowUsage();
}
}
finally{
Cleanup();
}
}
private static void ShowUsage(){
Console.WriteLine("Specify argument: 'add' or 'remove'.");
}
private static void InitializeEnvironment(){
Type dteType = Type.GetTypeFromProgID(DTE_PROG_ID);
dte = System.Activator.CreateInstance(dteType, true) as EnvDTE.DTE;
if(dte == null){
throw new ApplicationException("Unable to get DTE reference.");
}
toolboxTabName = GetTabName();
}
private static void Cleanup(){
if(dte != null){
dte.Quit();
Marshal.ReleaseComObject(dte);
}
}
private static void RemoveToolboxTab(){
EnvDTE.ToolBoxTab tab = GetToolBoxTab();
tab.Delete();
Console.WriteLine("Removed Tab.");
}
private static void AddItemsToToolbox() {
EnvDTE.ToolBoxTab newTab = GetToolBoxTab();
newTab.ToolBoxItems.Add(null, WEB_DLL_PATH, EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
Console.WriteLine("Added controls: {0}", WEB_DLL_PATH);
}
private static EnvDTE.ToolBox GetToolBox(){
EnvDTE.Window toolboxWindow = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
toolboxWindow.Visible = true;
toolboxWindow.DTE.ExecuteCommand("View.PropertiesWindow", "");
EnvDTE.ToolBox toolbox = toolboxWindow.Object as EnvDTE.ToolBox;
if(toolbox == null){
throw new ApplicationException("Unable to retrieve toolbox.");
}
return toolbox;
}
private static EnvDTE.ToolBoxTab GetToolBoxTab(){
EnvDTE.ToolBox toolbox = GetToolBox();
EnvDTE.ToolBoxTab tooltab = null;
string toolboxTabName = GetTabName();
foreach(EnvDTE.ToolBoxTab tab in toolbox.ToolBoxTabs){
Console.WriteLine("Tab: {0}", tab.Name);
if(tab.Name == toolboxTabName){
tooltab = tab;
}
}
if(tooltab == null){
tooltab = toolbox.ToolBoxTabs.Add(toolboxTabName);
Console.WriteLine("Added tab: {0}", tooltab.Name);
}
tooltab.Activate();
tooltab.ToolBoxItems.Item(1).Select();
return tooltab;
}
private static void EnumToolboxTabs(){
EnvDTE.ToolBox toolbox = GetToolBox();
EnvDTE.ToolBoxTab tooltab = null;
string toolboxTabName = GetTabName();
foreach(EnvDTE.ToolBoxTab tab in toolbox.ToolBoxTabs){
Console.WriteLine("Tab: {0}", tab.Name);
if(tab.Name == toolboxTabName){
tooltab = tab;
}
}
if(tooltab != null){
Console.WriteLine("Found the tab. Contents:");
foreach(EnvDTE.ToolBoxItem item in tooltab.ToolBoxItems){
Console.WriteLine("Item: {0}", item.Name);
}
}
}
private static string GetTabName(){
Assembly asm = Assembly.LoadFile(WEB_DLL_PATH);
Version ver = asm.GetName().Version;
string tabName = String.Format(TAB_NAME_FORMAT, ver.Major, ver.Minor, ver.Build);
return tabName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment