Getting Visual Studio installation directory

3 minute read

 

Determining the installation path of Visual Studio is something that sometimes is needed. For example we may we need to interact with Visual Studio executable (for example to reset visual studio or registering something on an installer).

But how can we determine where Visual Studio is installed? There are basically two ways of doing it.

  • Getting it from the environment variables.
  • Reading it from the registry

The path is stored in an environment variable called VSCOMNTOOLS where is the visual studio internal version number (multiplied by 100). This is not exactly the installation path but we can infer from it.

On my machine I have the following environment variables

VS100COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio 10.0Common7Tools
VS80COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio 8Common7Tools
VS90COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio 9.0Common7Tools

or we can get it from the registry the installation directory which is located on _HKLMSOFTWAREMicrosoftVisualStudioInstallDir_

This is true for 32 bit systems.

On 64 bit systems things are a little bit different. Since Visual Studio doesn’t directly support 64 bit systems, on 64 bit systems VS runs under the Windows on Windows system ( WoW64 ) so we must also that into account on 64 bit systems

in 64 bit systems _HKLMSOFTWAREWow6432NodeMicrosoftVisualStudioInstallDir_ But this only applies on 64 bit processes, if you are running under a 64 bit system but with a 32 bit process you don’t need to get the WoW registry key since windows transparently redirects registry accesses on that case (you can read more about redirection [here](http://msdn.microsoft.com/en-us/library/aa384232%28VS.85%29.aspx)).

If you follow these rules, you will be fine, but even better then a detailed description would be some running codeSmile

This little enumerate allows you to define the several versions in a more friendly way


/// <summary>
/// Represents the several visual studio versions.
/// 
/// Internally the value is stored as the VS internal number multiplied by 10 (eg: VS2010 is 100 since it's internal version number is 10.0)
/// </summary>
public enum VisualStudioVersion  { VS2010 = 100, VS2008 = 90 , VS2005 = 80, VSNet2003 = 71, VSNet2002 = 70, Other = 0};

And this code allows us to get the installation directory for the several VS versions


  using System;
  using Microsoft.Win32;u
  using System.Globalization;

  /// <summary>
  /// Gets the installation path of a given VS version.
  /// 
  /// Supported versions 2005, 2008 and 2010, .Net 2003 and .Net 2002
  /// </summary>
  internal static class VisualStudioHelper
  {
      /// <summary>
      /// Gets the installation directory of a given Visual Studio Version
      /// </summary>
      /// <param name="version">Visual Studio Version</param>
      /// <returns>Null if not installed the installation directory otherwise</returns>
      internal static string GetVisualStudioInstallationDir(VisualStudioVersion version)
      {
          string registryKeyString = String.Format(@"SOFTWARE{0}MicrosoftVisualStudio{1}",
              Environment.Is64BitProcess ? @"Wow6432Node" : "",
              GetVersionNumber(version));
  
          using (RegistryKey localMachineKey = Registry.LocalMachine.OpenSubKey(registryKeyString))
          {
              return localMachineKey.GetValue("InstallDir") as string;
          }
      }
  
      /// <summary>
      /// Gets the version number as used by Visual Studio to identify it's version internally.
      ///
      /// eg: Visual Studio 2010 is 10.0 and Visual Studio 2008 is 9.0
      /// </summary>
      /// <param name="version">Visual Studio Version</param>
      /// <returns>A string with the VS internal number version</returns>
      private static string GetVersionNumber(VisualStudioVersion version)
      {
          if (version == VisualStudioVersion.Other) throw new Exception("Not supported version");
  
          return ((int)version / 10).ToString("{0:0.0}", CultureInfo.InvariantCulture);
      }
  }

Executing the following code on a 64 bit operating system

Console.WriteLine(VisualStudioHelper.GetVisualStudioInstallationDir(VisualStudioVersion.VS2010));  
  
Console.WriteLine(VisualStudioHelper.GetVisualStudioInstallationDir(VisualStudioVersion.VS2008));

Console.WriteLine(VisualStudioHelper.GetVisualStudioInstallationDir(VisualStudioVersion.VS2005));

Console.WriteLine(VisualStudioHelper.GetVisualStudioInstallationDir(VisualStudioVersion.VSNet2003));

Will result in

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE

C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE

Yes I have all these versions running on my machine (I’ve used to have also VS .NET 2003 but on the last repave I didn’t reinstall itSmile )

Note: This code requires either .Net 3.5 or higher  since it uses  Environment.Is64BitProcess which is only been made available since .Net 3.5 if you want to run this with fwk 2.0 you will need to determine if we are running on a 64 bit process using another method.

Visual Studio internal version numbers are

Visual Studio 2010 10.0
Visual Studio 2008 9.0
Visual Studio 2005 8.0
Visual Studio .Net 2003 7.1
Visual Studio .Net 2002 7.0

Is this useful for something? Perhaps or perhaps not. But that is a history for another postSmile