Posted by: Brent Bishop

As Shane Tangen mentioned earlier this week, Platform Builder for CE 6.0 is integrated into Visual Studio 2005 as a VSIP plug-in. This integration makes it possible to create Visual Studio add-ins for Platform Builder.

This week we will focus on the basics by creating a simple Visual Studio add-in that will play a song when a build completes. We’ll dive into automating Platform Builder in the next post.

Step 1: Launch Visual Studio.

Step 2: Click File -> New -> Project.

Step 3: Select “Visual Studio Add-in” from the New Project dialog (under Other Project Types -> Extensibility as shown below) and give the project a name. In this case I’ve named the project BuildCompleteSample.

Visual Studio - New Project Wizard

Step 4: Visual Studio Add-in Wizard: Click next.

Step 5: Visual Studio Add-in Wizard (Page 1 of 6): Select “Create an Add-in using Visual C#” and click next.

Step 6: Visual Studio Add-in Wizard (Page 2 of 6): Deselect “Microsoft Visual Studio 2005 Macros” and click next.

Step 7: Visual Studio Add-in Wizard (Page 3 of 6): Add a name and description and then click next. For this sample I set them as shown below:

Name: Platform Builder - Build Complete Sample
Description: This sample shows how to generate an action when a build is completed.

Step 8: Visual Studio Add-in Wizard (Page 4 of 6): Select “I would like my Add-in to load when the host application starts” and click next.

Step 9: Visual Studio Add-in Wizard (Page 5 of 6): Click next or fill in the information to be included in the About dialog box if you wish.

Step 10: Click finish.

Step 11: Open Connect.cs and scroll to the bottom and add a private member variable to store a reference to the BuildEvents.

    private BuildEvents _buildEvents;

Step 12: Modify the OnStartupComplete method to get the reference to the BuildEvents and register an OnBuildDone event handler.

    public void OnStartupComplete(ref Array custom)
    {
        _buildEvents = _applicationObject.Events.BuildEvents;
        _buildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(_buildEvents_OnBuildDone);
    }

Step 13: Implement the event handler and add code to start your favorite sound. There are a lot of ways to play sounds but the following is a very simple way that gets the job done for this sample.

    /// <summary>
    /// The method that handles the on build done event.
    /// </summary>
    /// <param name="Scope">Represents the scope of the build.</param>
    /// <param name="Action">Represents the type of build action that is occurring, such as a build or a deploy action.</param>
    private void _buildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)
    {
        if (Action == vsBuildAction.vsBuildActionBuild)
        {
            // Get the path to the executing assembly
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            string path = System.IO.Path.GetDirectoryName(assembly.Location);

            // Launch the sound file
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = System.IO.Path.Combine(path, "BuildCompleteSample.wma");
            process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            process.Start();
        }
    }

Step 14: Compile the add-in and test it out.

That’s it. That’s all we need to have a fully functional Visual Studio add-in. Now that you’ve seen how to create an add-in for Visual Studio next time we’ll do something interesting with Platform Builder, create an OS design project.

For more information on Visual Studio add-ins check out the following resources: