是否可以在VS加载项中加载/卸载项目

时间:2013-04-05 12:08:47

标签: .net add-in

我想为VS编写一个加载项,在解决方案中选择项目,并卸载所有依赖它的项目。可能吗?

我知道有可能使用宏来实现它,但我想使用加载项来实现它。

1 个答案:

答案 0 :(得分:1)

这是解决方案

    private const string UnloadProjectCommandName = "Project.UnloadProject";
    private const string ReloadProjectCommandName = "Project.ReloadProject";

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
        handled = false;
        if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if (commandName == "Build7.Connect.Build7")
            {
                handled = true;
                var solution = (((SolutionClass)(_applicationObject.Solution)));

                var solutionExplorer = _applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
                solutionExplorer.Activate();

                var solutionHierarchy = (UIHierarchy)solutionExplorer.Object;
                string solutionName = solution.Properties.Item("Name").Value.ToString();

                var projects = solution.Projects;
                foreach (Project project in projects)
                {
                    var projPath = solutionName + "\\" + project.Name;
                    var obj = solutionHierarchy.GetItem(projPath);
                    obj.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    _applicationObject.ExecuteCommand(UnloadProjectCommandName);

                }

                foreach (Project project in projects)
                {
                    var projPath = solutionName + "\\" + project.Name;
                    var obj = solutionHierarchy.GetItem(projPath);
                    obj.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    _applicationObject.ExecuteCommand(ReloadProjectCommandName);

                }
            }
        }
    }

感谢Jeremy Jameson的post