Quick Tip: Programmatically retrieve Visual Studio install directory


Sometime we need to retrieve the Visual Studio installation directory programmatically using the C#/.NET code and there’s no direct API call available to retrieve this information and also you don’t know the exact path where it has been installed.

 

Every installation of Visual Studio puts a specific registry path to keep track of the installation directory. If you know that, it’s easy to retrieve the information. Let’s discuss on this topic today.

 

Programmatically retrieve Visual Studio install directory (www.kunal-chowdhury.com)

 

Every installation of Visual Studio puts the information related to installed directory in a specific registry path. It could be either “HKLM\SOFTWARE\Microsoft\VisualStudio\[Major.Minor]\InstallDir” or “HKML\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\[Major.Minor]\InstallDir”, based on 32-bit or 64-bit, where [Major.Minor] is the Visual Studio version no. For example, “12.0” denotes Visual Studio 2013 and “14.0” denotes Visual Studio 2015. In this registry path, “InstallDir” is the string value where it stores the exact path of the installation directory. Here is a screenshot of the same, for your reference:

 

Registry Path to retrieve Visual Studio installation directory (www.kunal-chowdhury.com)

 

To retrieve the same information programmatically, you have to write a piece of code (will demonstrate using C# here) and have to iterate through the said registry paths to find out the installed directory of the installed Visual Studio instance.

 

internal static string GetVisualStudioInstalledPath()
{
    var visualStudioInstalledPath = string.Empty;
    var visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0");
    if (visualStudioRegistryPath != null)
    {
        visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", string.Empty) 
as
string;
    }
 
    if(string.IsNullOrEmpty(visualStudioInstalledPath) || 
                            !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\14.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
string
.Empty) as string;
        }
    }
 
    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
string
.Empty) as string;
        }
    }
 
    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
string
.Empty) as string;
        }
    }
 
    return visualStudioInstalledPath;
}

 

Hope, this gave you basic idea on the topic and you will be able to use it directly or after optimization to get the desired output.  Do remember that, the code shared above is not optimized and kept in brief to give you clear picture of the same.

 

Also, the code shared here checks for installation directory of Visual Studio 2015 and Visual Studio 2013 only. If you want it to retrieve for other Visual Studio instance, give the appropriate version no. as Major.Minor, as mentioned above.

 

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top