When we open Package Manager Console in any open solution, it shows all the projects of that solution. How it is loading all the projects of the same solution. When I tried with below shown code it is fetching me projects of the first solution which I have opened.
private List<Project> GetProjects()
{
var dte = (DTE)Marshal.GetActiveObject(string.Format(CultureInfo.InvariantCulture, "VisualStudio.DTE.{0}.0", targetVsVersion));
var projects = dte.Solution.OfType<Project>().ToList();
return projects;
}
There may be a nicer way but I had a quick go at this and found this to work (it assumes you have a way of knowing the solution name). According to this post,
GetActiveObject
does not guarantee the current instance of VS which is why you're getting results from another instance. Instead, you can use theGetDTE
method shown there:If you know the solution name in advance, you can find it in the
MainWindowTitle
property ofProcess
and pass theProcessID
to the method above.Whilst the above code worked, I encountered a COM error which I fixed by using the
MessageFilter
class shown here.From that post, this is what the
MessageFilter
class looks likeThen you can access the project names like this
I believe you can use something like this:
Here are a various set of functions that allow you to enumerate projects in a given solution. This is how you would use it with the current solution: