代码之家  ›  专栏  ›  技术社区  ›  Artiom

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

  •  1
  • Artiom  · 技术社区  · 11 年前

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

    我知道有可能使用宏来制作,但我想使用外接程序来制作。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Artiom    11 年前

    这是解决方案

        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