Hello,
I encountered strange problem. When following code is compiled in Debug it works
ok. However when I compile it in Release. During uninstal VS .net 2003 display
crash warning. What is more no exception is thrown.
BTW addins are removed properly.
Someone has idea what I do wrong.
thanks
Szymek
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using EnvDTE;
using Microsoft.Office.Core;
namespace AddinRemover
{
public sealed class Remover
{
private Remover()
{
} // private Cleaner()
private const string VS_71_PROG_ID = "VisualStudio.DTE.7.1";
private const string VS_70_PROG_ID = "VisualStudio.DTE.7";
private const string ADDIN_NAME_COMMON_PART = "MyAddIn";
private const string CMD_BAR_NAME = "My";
private const string ADDIN_NAME = "My";
private static void RemoveAddin(string progId)
{
Type vsType = Type.GetTypeFromProgID(progId);
if (vsType != null)
{
object dteObj = Activator.CreateInstance(vsType);
if (dteObj != null)
{
DTE dte = (DTE)dteObj;
try
{
RemoveCommands(dte);
}
catch(Exception e)
{
Exception exp = e;
}
//dte.Quit();
dte = null;
GC.Collect();
}
}
}
private static void RemoveCommands(DTE dte)
{
Commands cmds = dte.Commands;
_CommandBars commandBars = dte.CommandBars;
try
{
CommandBar toolCommandBar = (CommandBar)commandBars["Tools"];
CommandBarControls ctrls = toolCommandBar.Controls;
IEnumerator en = ctrls.GetEnumerator();
while(en.MoveNext())
{
CommandBarControl ctrl = (CommandBarControl)en.Current;
if (CMD_BAR_NAME == ctrl.Caption)
{
ctrl.Delete(false);
}
}
}
catch(Exception /*e*/)
{
//TODO: logging
}
foreach(Command cmdDel in cmds)
{
if (cmdDel != null && cmdDel.Name != null &&
cmdDel.Name.IndexOf(ADDIN_NAME_COMMON_PART) >= 0)
{
try
{
cmdDel.Delete ( ) ;
}
catch(Exception /*e*/)
{
//TODO: logging
}
}
}
try
{
CommandBar cmdBar = (CommandBar)commandBars[CMD_BAR_NAME];
cmds.RemoveCommandBar(cmdBar);
}
catch(Exception /*e*/)
{
//TODO: logging
}
}
public static void Main(string[] args)
{
try
{
Remover.RemoveAddin(VS_70_PROG_ID);
}
catch(Exception /*e*/)
{
//TODO: logging
}
try
{
Remover.RemoveAddin(VS_71_PROG_ID);
}
catch(Exception /*e*/)
{
//TODO: logging
}
}
}