——RANT MODE: ON—–
In one C++ project I am currently working on (yes, C++ is not dead 😉 , I frequently have to switch between different startup projects in Visual Studio. Also, I sometimes want to start several executables at once. For this, Visual Studio supports setting of multiple startup projects.
However, settings those by hand can be a bit tedious, so I tried to make a visual basic macro for it. Ok, to be honest I did not try to make one – I tried to steal one from the internet.
A bit of googling revealed the following post which gives an approach for a single startup project and a “theoretical” solution for multiple startup projects:
http://matthiaskraaz.blogspot.com/2011/08/visual-studio-setting-multiple-startup.html
Turns out, setting multiple startup projects programmatically is not possible in Visual Studio 2005.
With Visual Studio 2010 however, it is possible. Here is how it works:
——RANT MODE: OFF—–
Public Sub SetMyStartupProjects() 'Create an array and fill it with the startup projects you would like to set Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), 3) StartupProjectsArray(0) = "MyProj1.vcxproj" StartupProjectsArray(1) = "MyProj2.vcxproj" StartupProjectsArray(2) = "MyProj3.vcxproj" 'Set the startup projects of the current solution Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild CurrentSolutionBuild.StartupProjects = StartupProjectsArray End sub
The code for creating the array might look a bit clunky, but a collection or any array defined via literals doesn’t work.
The type you use on the right hand side of CurrentSolutionBuild.StartupProjects =
really has to be of the type System.Array
and the things inside the array really need to have the type Object
. The rest is straightforward.
As a bonus, here is a little helper method that takes a usual VB collection:
Private Function SetStartupProjects(ByVal StartupProjects As Collection) Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), StartupProjects.Count) Dim i As Integer For i = 0 To StartupProjects.Count - 1 StartupProjectsArray(i) = StartupProjects(i + 1) Next Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild CurrentSolutionBuild.StartupProjects = StartupProjectsArray End Function
You can call it like this:
Public Sub SetMyStartupProjects() 'Create a collection that you would like to set Dim StartupProjects As New Collection StartupProjects.Add("MyProj1.vcxproj") StartupProjects.Add("MyProj2.vcxproj") StartupProjects.Add("MyProj3.vcxproj") 'Set the startup projects of the current solution SetStartupProjects(StartupProjects) End sub
I wonder why Microsoft doesn’t allow collections or straightforward String-arrays in the first place. Well, whatever, if you can encapsulate the ugly stuff, it is not ugly anymore.
Happy coding!