HOWTO: Create a solution from a Visual Studio package.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: July 2014   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013

This article explains how to create and save a solution from a Visual Studio package.

More Information

Although you can get an instance of the automation EnvDTE.DTE object from a package as explained in the article HOWTO: Get an EnvDTE.DTE instance from a Visual Studio package, and then use DTE.Solution and its Create and SaveAs methods, you can create and save a solution in a native way (without automation objects) using the Microsoft.VisualStudio.Shell.Interop.IVsSolution interface of the Microsoft.VisualStudio.Shell.Interop.SVsSolution service.

The following package sample shows how to create and save a solution:

Language: C#   Copy Code Copy Code (IE only)
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;

namespace Company.MyVSPackage
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   public sealed class MyVSPackagePackage : Package
      public MyVSPackagePackage()

      protected override void Initialize()


      private void CreateEmptySolution()
         const uint solutionCreationFlags = 0;

         SVsSolution solutionService;
         IVsSolution solutionInterface;
         string visualStudioProjectsDirectoryFullName;
         string solutionFullDirectoryName;
         string solutionFileName;
         bool isSolutionOpen;
         object value;

         // Get the Solution service
         solutionService = (SVsSolution)base.GetService(typeof(SVsSolution));

         // Get the Solution interface of the Solution service
         solutionInterface = solutionService as IVsSolution;

         // Get it there is a solution open
         solutionInterface.GetProperty((int) __VSPROPID.VSPROPID_IsSolutionOpen, out value);
         isSolutionOpen = (bool) value;

         if (isSolutionOpen)
            // Close the loaded solution
            solutionInterface.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_PromptSave, null, 0);

         // Get the folder where to save the solution
         visualStudioProjectsDirectoryFullName = GetVisualStudioProjectsFolder();
         solutionFullDirectoryName = System.IO.Path.Combine(visualStudioProjectsDirectoryFullName, "MySolution");
         solutionFileName = "MySolution.sln";

         // Create the folder if it doesn't exist
         if (!System.IO.Directory.Exists(solutionFullDirectoryName))

         // Create the solution
         solutionInterface.CreateSolution(solutionFullDirectoryName, solutionFileName, solutionCreationFlags);

         // Save the solution
         solutionInterface.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave, null, 0);

      private string GetVisualStudioProjectsFolder()
         SVsShell shellService;
         IVsShell shellInterface;
         string result = "";
         object value;

         // Get the Shell service
         shellService = (SVsShell)this.GetService(typeof(SVsShell));

         // Get the Shell interface of the Shell service
         shellInterface = shellService as IVsShell;

         if (shellInterface.GetProperty((int)__VSSPROPID.VSSPROPID_VisualStudioProjDir, out value) == Microsoft.VisualStudio.VSConstants.S_OK)
            result = (string)value;
         return result;


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)