Getting Visual Studio installation directory
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 VS
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 HKLMSOFTWAREMicrosoftVisualStudio
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 HKLMSOFTWAREWow6432NodeMicrosoftVisualStudio
If you follow these rules, you will be fine, but even better then a detailed description would be some running code
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 it )
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 |
Visual Studio 2008 |
Visual Studio 2005 |
Visual Studio .Net 2003 |
Visual Studio .Net 2002 |
Is this useful for something? Perhaps or perhaps not. But that is a history for another post