Keith Nicholas
Keith Nicholas

Reputation: 44288

Visual Studio Extensibility, How do you enumerate the projects in a solution?

Just trying to get up to speed with the SDK...

So, I've created my own tool window....

Now I want to iterate through the existing projects in the currently loaded solution and display their names in the tool window....

but not quite sure what the best way to enumerate the projects? any clues?

Upvotes: 5

Views: 1791

Answers (1)

Eugene Yokota
Eugene Yokota

Reputation: 95604

Check this code by Microsoft:

    static public IEnumerable<IVsProject> LoadedProjects
    {
        get
        {
            var solution = _serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
            if (solution == null)
            {
                Debug.Fail("Failed to get SVsSolution service.");
                yield break;
            }

            IEnumHierarchies enumerator = null;
            Guid guid = Guid.Empty;
            solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref guid, out enumerator);
            IVsHierarchy[] hierarchy = new IVsHierarchy[1] { null };
            uint fetched = 0;
            for (enumerator.Reset(); enumerator.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1; /*nothing*/)
            {
                yield return (IVsProject)hierarchy[0];
            }
        }
    }

Upvotes: 10

Related Questions