public void LoadAllProjectNodes()
        {
            string           solutionName = _app.Solution.Properties.Item("Name").Value.ToString();
            UIHierarchyItems items        = _rootNode.GetItem(solutionName).UIHierarchyItems;

            foreach (UIHierarchyItem topItem in items)
            {
                topItem.UIHierarchyItems.Expanded = true;
            }
        }
        internal void ReloadProject(int n)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeof(DTE));

            Project proj = dte.Solution.Projects.Item(1);

            string uniqueName = proj.UniqueName;
            string name       = proj.Name;

            IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsSolution));

            IVsHierarchy hier;

            Marshal.ThrowExceptionForHR(solutionService.GetProjectOfUniqueName(uniqueName, out hier));

            solutionService.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_UnloadProject, hier, 0);

            Window solutionExplorer = dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer) as Window;

            solutionExplorer.Activate();
            UIHierarchy uiHier = solutionExplorer.Object as UIHierarchy;

            UIHierarchyItem item = uiHier.GetItem(Path.GetFileNameWithoutExtension(dte.Solution.FileName) + "\\" + name);

            item.Select(vsUISelectionType.vsUISelectionTypeSelect);

            dte.ExecuteCommand("Project.ReloadProject", "");
        }
Example #3
0
        public static void SelectProject(this UIHierarchy hierarchy, Project project)
        {
            var solutionName = project.DTE.Solution.Item("Name");
            var projPath     = solutionName + "\\" + project.Name;
            var obj          = hierarchy.GetItem(projPath);

            obj.Select(vsUISelectionType.vsUISelectionTypeSelect);
        }
 public UIHierarchyItem GetItem(UIHierarchy root, string name)
 {
     try
     {
         return(root.GetItem(name));
     }
     catch (ArgumentException e)
     {
         throw new Failed(
                   "failed to get item {0}, {1}", name, e.Message);
     }
 }
Example #5
0
        /*
         *
         * /// <summary>
         *      /// Selects a project in the solution explorer.
         *      /// </summary>
         *      public static bool SelectProject(Project project)
         *      {
         *  if (project == null)
         *  {
         *      throw new ArgumentNullException("project");
         *  }
         *              // Select the parent folder to add the project to it.
         *              Window win = project.DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer);
         *  if (win == null)
         *  {
         *      // Can't select as there's no solution explorer open.
         *                      throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer);
         *  }
         *              win.Activate();
         *              win.SetFocus();
         *              UIHierarchy hier = win.Object as UIHierarchy;
         *              UIHierarchyItem sol = hier.UIHierarchyItems.Item(1);
         *  if (sol == null)
         *  {
         *      // No solution is opened.
         *      return false;
         *  }
         *              sol.Select(vsUISelectionType.vsUISelectionTypeSelect);
         *
         *              // Remove project file name from path.
         *              string name = Path.GetDirectoryName(project.UniqueName);
         *
         *              // Web projects can't be located through UniqueName.
         *              if (IsWebProject(project))
         *              {
         *                      // Locate by folder relative to solution one.
         *                      // WARNING: this will not work if project is NOT inside solution!
         *                      name = Path.GetDirectoryName(project.Properties.Item("FullPath").Value.ToString());
         *                      string slnpath = Path.GetDirectoryName(project.DTE.Solution.FullName);
         *                      name = name.Substring(name.IndexOf(slnpath) + slnpath.Length + 1);
         *              }
         *
         *              // Perform selection.
         *              UIHierarchyItem item = null;
         *              try
         *              {
         *                      item = hier.GetItem(Path.Combine(sol.Name, name));
         *              }
         *              catch (ArgumentException)
         *              {
         *                      // Retry selection by name (much slower!)
         *                      item = FindProjectByName(project.Name, hier.UIHierarchyItems);
         *              }
         *  if (item != null)
         *  {
         *      item.UIHierarchyItems.Expanded = true;
         *      item.Select(vsUISelectionType.vsUISelectionTypeSelect);
         *      return true;
         *  }
         *  else
         *  {
         *      return false;
         *  }
         *      }
         *
         *      private static UIHierarchyItem FindProjectByName(string name, UIHierarchyItems items)
         *      {
         *              foreach (UIHierarchyItem item in items)
         *              {
         *                      if (item.Name == name)
         *                      {
         *                              ProjectItem pi = item.Object as ProjectItem;
         *                              // Check the project name or the subproject name (for ETP).
         *                              if (pi.ContainingProject.Name == name ||
         *                                      (pi.SubProject != null && pi.SubProject.Name == name))
         *                                      return item;
         *                      }
         *
         *                      if (item.UIHierarchyItems != null)
         *                      {
         *                              UIHierarchyItem uii = FindProjectByName(name, item.UIHierarchyItems);
         *                              if (uii != null) return uii;
         *                      }
         *              }
         *
         *              return null;
         *      }
         *
         */

        #endregion Removed feature - not working quite good

        /// <summary>
        /// Selects a solution explorer item, based on
        /// its relative path with regards to the solution.
        /// </summary>
        /// <remarks>
        /// If selection fails, returned object will be null too.
        /// </remarks>
        public static UIHierarchyItem SelectItem(_DTE vs, string path)
        {
            if (vs == null)
            {
                throw new ArgumentNullException("vs");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            // Select the parent folder to add the project to it.
            Window win = vs.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer);

            if (win == null)
            {
                // Can't select as there's no solution explorer open.
                throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer);
            }
            win.Activate();
            win.SetFocus();
            UIHierarchy     hier = win.Object as UIHierarchy;
            UIHierarchyItem sol  = hier.UIHierarchyItems.Item(1);

            if (sol == null)
            {
                // No solution is opened.
                throw new InvalidOperationException(Properties.Resources.DteHelper_NoSolutionExplorer);
            }
            sol.Select(vsUISelectionType.vsUISelectionTypeSelect);
            // Perform selection.
            UIHierarchyItem item = null;

            try
            {
                string slnpath = Path.Combine(sol.Name, path);
                item = hier.GetItem(slnpath);
            }
            catch (ArgumentException)
            {
                // Retry selection by name (slower)
                item = FindHierarchyItemByPath(sol.UIHierarchyItems,
                                               path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), 0);
            }

            if (item != null)
            {
                item.UIHierarchyItems.Expanded = true;
                item.Select(vsUISelectionType.vsUISelectionTypeSelect);
            }

            return(item);
        }
        static UIHierarchyItem GetItemWorkaround(UIHierarchy uh, string item)
        {
            var             items = item.Split('\\');
            UIHierarchyItem uii   = null;

            foreach (var s in items)
            {
                uii = uii == null?uh.GetItem(s) : uii.UIHierarchyItems.Item(s);

                uii.Select(vsUISelectionType.vsUISelectionTypeSelect);
            }

            return(uii);
        }
Example #7
0
        /// <summary>
        /// 折叠全部
        /// </summary>
        public static void CollapseAll()
        {
            _dte      = SolutionCommon.Dte as DTE2;
            _rootNode = _dte.ToolWindows.SolutionExplorer;

            List <UIHierarchyItem> itemNodes = GetProjectNodes();

            if (itemNodes != null && itemNodes.Count > 0)
            {
                for (int i = 0; i < itemNodes.Count; i++)
                {
                    Project proj = itemNodes[i].Object as Project;
                    //项目为非顶级项目
                    if (proj == null)
                    {
                        ProjectItem projItem = itemNodes[i].Object as ProjectItem;
                        if (projItem != null && itemNodes[i].UIHierarchyItems.Expanded)
                        {
                            itemNodes[i].Select(vsUISelectionType.vsUISelectionTypeSelect);
                            _rootNode.DoDefaultAction();
                        }
                    }
                    else
                    {
                        //项目为顶级项目
                        itemNodes[i].UIHierarchyItems.Expanded = false;
                    }
                }
            }
            //折叠顶级项目
            string           solutionName = _dte.Solution.Properties.Item("Name").Value.ToString();
            UIHierarchyItems items        = _rootNode.GetItem(solutionName).UIHierarchyItems;

            foreach (UIHierarchyItem topItem in items)
            {
                topItem.UIHierarchyItems.Expanded = false;
            }
        }