Export (0) Print
Expand All

How to: Obtain the Solution Startup Project

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see How to: Convert an Add-in to a VSPackage.

The Startup Object property for a project defines the entry point to be called when the application loads. This is usually set to either the main form in your application or to the procedure that you want to run when the application starts. For more information about startup objects, see How to: Change the Startup Object for an Application (Visual Basic).

Similarly, you can set a startup project for your solution. A solution can contain one or more projects, and each project can in turn define a startup object, such as a form or a procedure. A solution, however, can have only one startup project which you can retrieve using the StartupProjects property.

StartupProjects returns an object that is an array of strings, each one the name of a startup project. If you just need the name of the solution startup project, then you can just iterate through the array. If you need to reference the startup project, however, you need to cast it back to a Project object.

Note Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings.

Obtaining the solution startup project

  1. Start Visual Studio and load a solution containing one or more projects.

  2. Get a reference to the SolutionBuild2 object, such as:

    SolutionBuild2 sb = (SolutionBuild2)dte.Solution.SolutionBuild;
    
  3. Iterate through the solution's StartupProject collection:

    foreach (String item in (Array)sb.StartupProjects)
    {
        msg += item;
    }
    
  4. Pass the name (which is in string form) of the startup project as the index to DTE.Solution.Item to reference the startup project.

The following example shows how to retrieve the name of the startup project as well as how to use that name to reference the startup project itself.

Before running this example, be sure to load a solution containing one or more projects.

public void getStartupSolnProject(DTE2 dte)
{
    // Gets the name of the startup project for the solution
    // and then casts it to a Project object.
    // Have a project loaded before running this code.
    // Only one project in a solution can be a startup project.
    SolutionBuild2 sb = (SolutionBuild2)dte.Solution.SolutionBuild;
    Project startupProj;
    string msg = "";

    foreach (String item in (Array)sb.StartupProjects)
    {
        msg += item;
    }
    System.Windows.Forms.MessageBox.Show("Solution startup Project: 
    "+msg);
    startupProj = dte.Solution.Item(msg);
    System.Windows.Forms.MessageBox.Show("Full name of solution's 
    startup project: "+"/n"+startupProj.FullName);
}
Show:
© 2014 Microsoft