Logo
HOWTO: Get information about the Visual Studio IDE 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
Introduction

This article explains how to get information about properties of the Visual Studio IDE from a 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 use its properties (such as DTE.Name, DTE.Version, DTE.RegistryRoot, etc.), you can get information in a native way (without automation objects) using the Microsoft.VisualStudio.Shell.Interop.IVsShell interface of the Microsoft.VisualStudio.Shell.Interop.SVsShell service and its GetProperty method with the enum values provided by:

Note: The Visual Studio "Shell" is the core IDE. Most of the functionality of Visual Studio is provided by packages that are added to the shell.

The following package sample shows how to get some properties of the Visual Studio IDE:

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
{
   [ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]
   [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [Guid(GuidList.guidMyVSPackagePkgString)]
   public sealed class MyVSPackagePackage : Package
   {
      public MyVSPackagePackage()
      {
      }

      protected override void Initialize()
      {
         base.Initialize();

         ShowIdeProperties();
      }

      private void ShowIdeProperties()
      {
         SVsShell shellService;
         IVsShell shellInterface;
         string installDir;
         string registryRoot;
         string projectsFolder;
         string visualStudioFolder;
         bool shellInitialized;

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

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

         // Get some properties

         installDir = GetPropertyValue<string>(shellInterface, (int)__VSSPROPID.VSSPROPID_InstallDirectory);
         MessageBox.Show("Install Dir: " + installDir);

         registryRoot = GetPropertyValue<string>(shellInterface, (int)__VSSPROPID.VSSPROPID_VirtualRegistryRoot);
         MessageBox.Show("Registry Root: " + registryRoot);

         projectsFolder = GetPropertyValue<string>(shellInterface, (int)__VSSPROPID.VSSPROPID_VisualStudioProjDir);
         MessageBox.Show("Projects folder: " + projectsFolder);

         visualStudioFolder = GetPropertyValue<string>(shellInterface, (int)__VSSPROPID2.VSSPROPID_VisualStudioDir);
         MessageBox.Show("Visual Studio user folder: " + visualStudioFolder);

         shellInitialized = GetPropertyValue<bool>(shellInterface, (int)__VSSPROPID4.VSSPROPID_ShellInitialized);
         MessageBox.Show("Shell initialized: " + shellInitialized);
      }

      private T GetPropertyValue<T>(IVsShell shellInterface, int property)
      {
         object value = null;
         T result = default(T);

         if (shellInterface.GetProperty(property, out value) == Microsoft.VisualStudio.VSConstants.S_OK)
         {
            result = (T)value;
         }
         return result;
      }
   }
}


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