Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of projects in current Visual studio solution?

Tags:

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;     } 
like image 769
Palak.Maheria Avatar asked Mar 28 '14 05:03

Palak.Maheria


People also ask

How do I see all projects in Visual Studio?

Visual Studio 17.2 Preview 3 introduces a brand-new All-In-One search experience that merges the existing VS Search (Ctrl + Q) and Go To (Ctrl + T) to allow you to search both your code and Visual Studio features quicker and easier than ever, all in the same place.

Who displays the list of projects contained in current solution?

Who displays the list of projects contained in current solution? Explanation: The Solution Explorer Window provides you with an organized view of your projects and their files as well as ready access to the commands that pertain to them.

How do I view multiple projects in Visual Studio?

Setting multiple startup projects in Visual Studio is easy… 1 – Right click the solution and select 'Set Startup Projects…'. 2 – Select 'Multiple startup projects' and choose two or more projects. 3 – Press run and Visual Studio will open them in your selected browser.

Where are my Visual Studio projects located?

When you create a new project, Visual Studio saves it to its default location, %USERPROFILE%\source\repos. To change this location, go to Tools > Options > Projects and Solutions > Locations. For more information, see Options dialog box: Projects and Solutions > Locations.


2 Answers

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 the GetDTE method shown there:

[DllImport("ole32.dll")] private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);  public static DTE GetDTE(int processId) {     string progId = "!VisualStudio.DTE.10.0:" + processId.ToString();     object runningObject = null;      IBindCtx bindCtx = null;     IRunningObjectTable rot = null;     IEnumMoniker enumMonikers = null;      try     {         Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx));         bindCtx.GetRunningObjectTable(out rot);         rot.EnumRunning(out enumMonikers);          IMoniker[] moniker = new IMoniker[1];         IntPtr numberFetched = IntPtr.Zero;         while (enumMonikers.Next(1, moniker, numberFetched) == 0)         {             IMoniker runningObjectMoniker = moniker[0];              string name = null;              try             {                 if (runningObjectMoniker != null)                 {                     runningObjectMoniker.GetDisplayName(bindCtx, null, out name);                 }             }             catch (UnauthorizedAccessException)             {                 // Do nothing, there is something in the ROT that we do not have access to.             }              if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal))             {                 Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject));                 break;             }         }     }     finally     {         if (enumMonikers != null)         {             Marshal.ReleaseComObject(enumMonikers);         }          if (rot != null)         {             Marshal.ReleaseComObject(rot);         }          if (bindCtx != null)         {             Marshal.ReleaseComObject(bindCtx);         }     }      return (DTE)runningObject; }  

If you know the solution name in advance, you can find it in the MainWindowTitle property of Process and pass the ProcessID to the method above.

var dte = GetDTE(System.Diagnostics.Process.GetProcesses().Where(x => x.MainWindowTitle.StartsWith("SolutionName") && x.ProcessName.Contains("devenv")).FirstOrDefault().Id); 

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 like

public class MessageFilter : IOleMessageFilter {                 // Class containing the IOleMessageFilter     // thread error-handling functions.      // Start the filter.     public static void Register()     {         IOleMessageFilter newFilter = new MessageFilter();         IOleMessageFilter oldFilter = null;         CoRegisterMessageFilter(newFilter, out oldFilter);     }       // Done with the filter, close it.     public static void Revoke()     {         IOleMessageFilter oldFilter = null;         CoRegisterMessageFilter(null, out oldFilter);     }       //     // IOleMessageFilter functions.     // Handle incoming thread requests.     int IOleMessageFilter.HandleInComingCall(int dwCallType,     System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr lpInterfaceInfo)     {          //Return the flag SERVERCALL_ISHANDLED.         return 0;     }       // Thread call was rejected, so try again.     int IOleMessageFilter.RetryRejectedCall(System.IntPtr     hTaskCallee, int dwTickCount, int dwRejectType)     {          if (dwRejectType == 2)         // flag = SERVERCALL_RETRYLATER.         {             // Retry the thread call immediately if return >=0 &             // <100.             return 99;         }         // Too busy; cancel call.         return -1;     }       int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,     int dwTickCount, int dwPendingType)     {         //Return the flag PENDINGMSG_WAITDEFPROCESS.         return 2;     }       // Implement the IOleMessageFilter interface.     [DllImport("Ole32.dll")]     private static extern int       CoRegisterMessageFilter(IOleMessageFilter newFilter, out       IOleMessageFilter oldFilter); }    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] interface IOleMessageFilter {     [PreserveSig]     int HandleInComingCall(     int dwCallType,     IntPtr hTaskCaller,     int dwTickCount,     IntPtr lpInterfaceInfo);      [PreserveSig]     int RetryRejectedCall(     IntPtr hTaskCallee,     int dwTickCount,     int dwRejectType);       [PreserveSig]     int MessagePending(         IntPtr hTaskCallee,         int dwTickCount,         int dwPendingType); } 

Then you can access the project names like this

var dte = GetDTE(System.Diagnostics.Process.GetProcesses().Where(x => x.MainWindowTitle.StartsWith("SolutionName") && x.ProcessName.Contains("devenv")).FirstOrDefault().Id); MessageFilter.Register(); var projects = dte.Solution.OfType<Project>().ToList(); MessageFilter.Revoke();  foreach (var proj in projects) {    Debug.WriteLine(proj.Name); }  Marshal.ReleaseComObject(dte); 
like image 26
keyboardP Avatar answered Oct 16 '22 18:10

keyboardP


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:

// get current solution IVsSolution solution = (IVsSolution)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(IVsSolution)); foreach(Project project in GetProjects(solution)) {     .... }  ....  public static IEnumerable<EnvDTE.Project> GetProjects(IVsSolution solution) {     foreach (IVsHierarchy hier in GetProjectsInSolution(solution))     {         EnvDTE.Project project = GetDTEProject(hier);         if (project != null)             yield return project;     } }  public static IEnumerable<IVsHierarchy> GetProjectsInSolution(IVsSolution solution) {     return GetProjectsInSolution(solution, __VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION); }  public static IEnumerable<IVsHierarchy> GetProjectsInSolution(IVsSolution solution, __VSENUMPROJFLAGS flags) {     if (solution == null)         yield break;      IEnumHierarchies enumHierarchies;     Guid guid = Guid.Empty;     solution.GetProjectEnum((uint)flags, ref guid, out enumHierarchies);     if (enumHierarchies == null)         yield break;      IVsHierarchy[] hierarchy = new IVsHierarchy[1];     uint fetched;     while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1)     {         if (hierarchy.Length > 0 && hierarchy[0] != null)             yield return hierarchy[0];     } }  public static EnvDTE.Project GetDTEProject(IVsHierarchy hierarchy) {     if (hierarchy == null)         throw new ArgumentNullException("hierarchy");      object obj;     hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);     return obj as EnvDTE.Project; } 
like image 126
Simon Mourier Avatar answered Oct 16 '22 16:10

Simon Mourier